Skip to content

Instantly share code, notes, and snippets.

@pallas
Created November 21, 2016 00:52
Embed
What would you like to do?
C++ macros for trying libc calls, throwing std::runtime_error on errno
#ifndef TRY_H
#define TRY_H
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <errno.h>
#define TRY(f, ...) ({ \
typeof (f(__VA_ARGS__)) _r = f(__VA_ARGS__); \
if (_r < 0) perror(#f), throw std::runtime_error(strerror(errno)); \
_r; \
})
#define TRY_ERR(e, f, ...) ({ \
typeof (f(__VA_ARGS__)) _r = f(__VA_ARGS__); \
if (_r < 0 && errno != e) perror(#f), throw std::runtime_error(strerror(errno)); \
_r; \
})
#define TRY_PTR(f, ...) ({ \
typeof (f(__VA_ARGS__)) _r = f(__VA_ARGS__); \
if (!_r) perror(#f), throw std::runtime_error(strerror(errno)); \
_r; \
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment