generic_errors.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Simple test file for cxxomfort's
  3. * system_error feature backports.
  4. *
  5. * This example draws from the example posted in
  6. * https://en.cppreference.com/w/cpp/error/generic_category
  7. *
  8. */
  9. #include <cxxomfort/base.hpp>
  10. #include <cxxomfort/system_error.hpp>
  11. #include <cxxomfort/string_view.hpp>
  12. #include <cxxomfort/ostream.hpp>
  13. #include <iostream>
  14. #include <cerrno>
  15. #include <string>
  16. #include <fstream>
  17. #include <cxxomfort/library/type_name.hpp>
  18. int main()
  19. {
  20. using namespace std;
  21. using cxxomfort::library::type_name;
  22. cxxomfort::output_info();
  23. cout<< endl;
  24. cout<< "error_category is: "<< type_name<error_category>()<< endl;
  25. error_category const& ecat1 = system_category();
  26. error_category const& ecat2 = generic_category();
  27. cout<< "system == generic? "<< (ecat1==ecat2)<< endl;
  28. error_condition gcond = generic_category().default_error_condition(ENOENT);
  29. cout<< "Generic :"<< endl;
  30. cout<< "Category : " << gcond.category().name() << '\n'
  31. << "Value : " << gcond.value() << '\n'
  32. << "Message : " << gcond.message() << '\n';
  33. error_condition scond = system_category().default_error_condition(EDOM);
  34. cout<< "System :"<< endl;
  35. cout<< "Category : " << scond.category().name() << '\n'
  36. << "Value : " << scond.value() << '\n'
  37. << "Message : " << scond.message() << '\n';
  38. cout<< "Malformed error code:"<< endl;
  39. error_condition wrongcond = system_category().default_error_condition(10001);
  40. cout<< "Category: " << wrongcond.category().name() << '\n'
  41. << "Value: " << wrongcond.value() << '\n'
  42. << "Message: " << wrongcond.message() << '\n';
  43. cout<< "\n"
  44. << "Making an error_code..."<< flush;
  45. error_code ec = make_error_code(errc::identifier_removed);
  46. cout<< "Done"<< endl;
  47. cout<< ec<< " , "<< ec.message()<< endl;
  48. cout<< make_error_code(cxxomfort::fix::to_errc(14))<< endl;
  49. cout<< endl;
  50. cout<< "Type of std::errc : "<<
  51. #if (defined(CXXO_FIX_broken_msvc_errc))
  52. "bad (declares a namespace)"
  53. #else
  54. type_name<std::errc>()
  55. #endif
  56. << endl;
  57. cout<< "Type of cxxomfort errc : "<< type_name<cxxomfort::fix::errc>()<< endl;
  58. cxxomfort::fix::errc ex;
  59. (void)ex;
  60. try {
  61. cout<< "Trying to trigger an error."<< endl;
  62. throw system_error(EBUSY, system_category());
  63. }
  64. catch (std::system_error const& E) {
  65. cout << "Caught a system_error\n";
  66. cout << "Code "<< E.code()<< " , dynamic type "<< cxxomfort::library::typeid_demangle(typeid(E))<< endl;
  67. cout<< " Error description " << E.what() << endl;
  68. }
  69. catch (std::exception const& E) {
  70. cout<< "Caught a generic exception "<< E.what()<< endl;
  71. cout<< "Dynamic exception type: "<< cxxomfort::library::typeid_demangle(typeid(E))<< endl;
  72. } catch (...) {
  73. cout<< "Unknown exception"<< endl;
  74. }
  75. }