exception.cc 687 B

123456789101112131415161718192021222324252627282930313233
  1. #include "exception.hh"
  2. #include <cstdio>
  3. #include <cstring>
  4. errno_exception::errno_exception(int errno)
  5. : _errno(errno)
  6. {
  7. }
  8. int errno_exception::errno() const
  9. {
  10. return _errno;
  11. }
  12. const char *errno_exception::what()
  13. {
  14. std::snprintf(_buf, sizeof _buf, "error: %s (%d)",
  15. std::strerror(_errno), _errno);
  16. return _buf;
  17. }
  18. int try_main(int (*main)(int argc, char** argv), int argc, char** argv,
  19. int ret_on_exception)
  20. {
  21. try {
  22. return main(argc, argv);
  23. } catch (std::exception& e) {
  24. std::fprintf(stderr, "exception: %s\n", e.what());
  25. } catch (...) {
  26. std::fprintf(stderr, "unknown exception\n");
  27. }
  28. return ret_on_exception;
  29. }