{"id":5060,"date":"2022-09-17T19:45:35","date_gmt":"2022-09-17T18:45:35","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=5060"},"modified":"2022-09-17T19:45:35","modified_gmt":"2022-09-17T18:45:35","slug":"c-guns-throw-and-catch-all-standard-exceptions-for-fun","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=5060","title":{"rendered":"C++ Guns: throw and catch all standard exceptions for fun"},"content":{"rendered":"<p>This example throw and catch all standard exceptions just for fun<br \/>\nexception list from https:\/\/en.cppreference.com\/w\/cpp\/error\/exception<br \/>\nsorted after C++ Standard<\/p>\n<p>searching a stack trace? look at https:\/\/en.cppreference.com\/w\/cpp\/utility\/basic_stacktrace<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* This example throw and catch all standard exceptions just for fun\r\n * exception list from https:\/\/en.cppreference.com\/w\/cpp\/error\/exception\r\n * sorted after C++ Standard\r\n *\r\n * searching a stack trace? look at https:\/\/en.cppreference.com\/w\/cpp\/utility\/basic_stacktrace\r\n *\/\r\n\r\n#include &lt;iostream&gt;\r\n#include &lt;exception&gt;\r\n#include &lt;future&gt;\r\n#include &lt;regex&gt;\r\n#include &lt;filesystem&gt;\r\n#include &lt;chrono&gt;\r\n#include &lt;any&gt;\r\n#include &lt;optional&gt;\r\n#include &lt;variant&gt;\r\n\/\/#include &lt;format&gt; \/\/ C++20. Not implemented in GCC 12\r\n\/\/ #include &lt;expected&gt; \/\/ C++23\r\n\r\n\r\nint nCatched = 0;\r\n\r\ntemplate&lt;typename T&gt;\r\nvoid check(const T&amp; ex, std::string_view exceptionType) {\r\n  nCatched++;\r\n  std::string_view what(ex.what());\r\n  \/\/ only compare the first chars from what() message, we are not intereseting in stuff after the exception type\r\n  if(what.substr(0,exceptionType.size()) != exceptionType) {\r\n      std::cerr &lt;&lt; exceptionType &lt;&lt; &quot; what(): &quot; &lt;&lt; ex.what() &lt;&lt; &quot;\\n&quot;;\r\n      std::cerr &lt;&lt; &quot;\\nERROR: Not all exception derived from &quot; &lt;&lt; exceptionType &lt;&lt; &quot; catched...\\n&quot;;\r\n      ::exit(EXIT_FAILURE);\r\n    }\r\n}\r\n\r\nint main() {\r\n\r\n  bool finish = false;\r\n  int nThrow = 0;\r\n  while(not finish){\r\n    try {\r\n      switch(nThrow) {\r\n        case  1: ++nThrow; throw std::exception();\r\n        case  2: ++nThrow; throw std::logic_error(&quot;std::logic_error&quot;);\r\n        case  3: ++nThrow; throw std::invalid_argument(&quot;std::invalid_argument&quot;);\r\n        case  4: ++nThrow; throw std::domain_error(&quot;std::domain_error&quot;);\r\n        case  5: ++nThrow; throw std::length_error(&quot;std::length_error&quot;);\r\n        case  6: ++nThrow; throw std::out_of_range(&quot;std::out_of_range&quot;);\r\n        case  7: ++nThrow; throw std::runtime_error(&quot;std::runtime_error&quot;);\r\n        case  8: ++nThrow; throw std::range_error(&quot;std::range_error&quot;);\r\n        case  9: ++nThrow; throw std::overflow_error(&quot;std::overflow_error&quot;);\r\n        case 10: ++nThrow; throw std::underflow_error(&quot;std::underflow_error&quot;);\r\n        case 11: ++nThrow; throw std::bad_typeid();\r\n        case 12: ++nThrow; throw std::bad_alloc();\r\n        case 13: ++nThrow; throw std::bad_exception();\r\n        case 14: ++nThrow; throw std::regex_error(std::regex_constants::error_collate); \/\/ C++11\r\n        case 15: ++nThrow; throw std::system_error(ENOENT, std::system_category(), &quot;std::system_error&quot;); \/\/ C++11\r\n        case 16: ++nThrow; throw std::ios_base::failure(&quot;std::ios_base::failure&quot;); \/\/ C++11\r\n        case 17: ++nThrow; throw std::future_error(std::future_errc::broken_promise); \/\/ C++11\r\n        case 18: ++nThrow; throw std::bad_weak_ptr(); \/\/ C++11\r\n        case 19: ++nThrow; throw std::bad_function_call(); \/\/ C++11\r\n        case 20: ++nThrow; throw std::bad_array_new_length(); \/\/ C++11\r\n        case 21: ++nThrow; throw std::filesystem::filesystem_error(&quot;std::filesystem::filesystem_error&quot;, std::error_code(ENOENT, std::system_category())); \/\/ C++17\r\n        case 22: ++nThrow; throw std::bad_any_cast(); \/\/ C++17\r\n        case 23: ++nThrow; throw std::bad_optional_access(); \/\/ C++17\r\n        case 24: ++nThrow; throw std::bad_variant_access(); \/\/ C++17\r\n        \/\/ case 25: throw std::chrono::nonexistent_local_time(); \/\/ C++ 20. Not implemented in GCC 12\r\n        \/\/ case 26: throw std::chrono::ambiguous_local_time(); \/\/ C++20. Not implemented in GCC 12\r\n        \/\/ case 27: throw std::format_error(); \/\/ C++20. Not implemented in GCC 12\r\n        \/\/ case 28: throw std::bad_expected_access(); \/\/ C++23\r\n        \/\/ case 29: throw std::tx_exception(); TODO\r\n        default: {\r\n          finish = true;\r\n        }\r\n      }\r\n    }\r\n    catch(std::bad_variant_access&amp; ex) {\r\n      check(ex, &quot;bad variant access&quot;);\r\n    }\r\n    catch(std::bad_exception&amp; ex) {\r\n      check(ex, &quot;std::bad_exception&quot;);\r\n    }\r\n    catch(std::bad_array_new_length&amp; ex) {\r\n      check(ex, &quot;std::bad_array_new_length&quot;);\r\n    }\r\n    catch(std::bad_alloc&amp; ex) {\r\n      check(ex, &quot;std::bad_alloc&quot;);\r\n    }\r\n    catch(std::bad_function_call&amp; ex) {\r\n      check(ex, &quot;bad_function_call&quot;);\r\n    }\r\n    catch(std::bad_weak_ptr&amp; ex) {\r\n      check(ex, &quot;bad_weak_ptr&quot;);\r\n    }\r\n    catch(std::bad_optional_access&amp; ex) {\r\n      check(ex, &quot;bad optional access&quot;);\r\n    }\r\n    catch(std::bad_any_cast&amp; ex) {\r\n      check(ex, &quot;bad any_cast&quot;);\r\n    }\r\n    catch(std::bad_typeid&amp; ex) {\r\n      check(ex, &quot;std::bad_typeid&quot;);\r\n    }\r\n    catch(std::filesystem::filesystem_error&amp; ex) {\r\n      check(ex, &quot;filesystem error&quot;);\r\n    }\r\n    catch(std::ios_base::failure&amp; ex) {\r\n      check(ex, &quot;std::ios_base::failure&quot;);\r\n    }\r\n    catch(std::system_error&amp; ex) {\r\n      check(ex, &quot;std::system_error&quot;);\r\n    }\r\n    catch(std::regex_error&amp; ex) {\r\n      check(ex, &quot;Invalid collating element in regular expression&quot;); \/\/ regex_error.what does not print the exception type first...\r\n    }\r\n    catch(std::underflow_error&amp; ex) {\r\n      check(ex, &quot;std::underflow_error&quot;);\r\n    }\r\n    catch(std::overflow_error&amp; ex) {\r\n      check(ex, &quot;std::overflow_error&quot;);\r\n    }\r\n    catch(std::range_error&amp; ex) {\r\n      check(ex, &quot;std::range_error&quot;);\r\n    }\r\n    catch(std::runtime_error&amp; ex) {\r\n      check(ex, &quot;std::runtime_error&quot;);\r\n    }\r\n    catch(std::future_error&amp; ex) {\r\n      check(ex, &quot;std::future_error&quot;);\r\n    }\r\n    catch(std::out_of_range&amp; ex) {\r\n      check(ex, &quot;std::out_of_range&quot;);\r\n    }\r\n    catch(std::length_error&amp; ex) {\r\n      check(ex, &quot;std::length_error&quot;);\r\n    }\r\n    catch(std::domain_error&amp; ex) {\r\n      check(ex, &quot;std::domain_error&quot;);\r\n    }\r\n    catch(std::invalid_argument&amp; ex) {\r\n      check(ex, &quot;std::invalid_argument&quot;);\r\n    }\r\n    catch(std::logic_error&amp; ex) {\r\n      check(ex, &quot;std::logic_error&quot;);\r\n    }\r\n    catch(std::exception&amp; ex) {\r\n      check(ex, &quot;std::exception&quot;);\r\n    }\r\n  } \/\/ while\r\n\r\n\r\n  if(nThrow != nCatched) {\r\n    std::cerr &lt;&lt; nThrow &lt;&lt; &quot; exception thrown but &quot; &lt;&lt; nCatched &lt;&lt; &quot; catched\\n&quot;;\r\n  } else {\r\n    std::cout &lt;&lt; &quot;All exceptions which was thrown was catched\\n&quot;;\r\n  }\r\n\r\n  return EXIT_SUCCESS;\r\n}\r\n<\/pre>\n<blockquote><p>\n $ .\/a.out<br \/>\nAll exceptions which was thrown was catched\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This example throw and catch all standard exceptions just for fun exception list from https:\/\/en.cppreference.com\/w\/cpp\/error\/exception sorted after C++ Standard searching a stack trace? look at https:\/\/en.cppreference.com\/w\/cpp\/utility\/basic_stacktrace \/* This example throw and catch all standard exceptions just for fun * exception list from https:\/\/en.cppreference.com\/w\/cpp\/error\/exception * sorted after C++ Standard * * searching a stack trace? look [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-5060","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5060"}],"version-history":[{"count":2,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5060\/revisions"}],"predecessor-version":[{"id":5062,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5060\/revisions\/5062"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5060"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}