{"id":5285,"date":"2024-04-03T21:58:05","date_gmt":"2024-04-03T20:58:05","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=5285"},"modified":"2024-04-03T21:58:05","modified_gmt":"2024-04-03T20:58:05","slug":"5285","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=5285","title":{"rendered":""},"content":{"rendered":"<p>Mal ausprobieren ob damit Exception einfacher in der Handhabung werden...<br \/>\nAus <a href=\"https:\/\/www.youtube.com\/watch?v=Oy-VTqz1_58&pp=ygURZXhjZXB0aW9uYWxsIHliYWQ%3D\">Exceptionally Bad: The Misuse of Exceptions in C++ & How to Do Better - Peter Muldoon - CppCon 2023<\/a><\/p>\n<p>Conclusion of the talk:<\/p>\n<p>What should they used for ?<br \/>\n\u2022 Error tracing (logging)<br \/>\n\u2022 Stack unwinding (reset \/ termination)<br \/>\n\u2022 Data passing \/ Control flow (when necessary)<\/p>\n<p>When should they used ?<br \/>\n\u2022 As \u201cRarely\u201d as possible<br \/>\n\u2022 Serious\/infrequent\/unexpected errors<br \/>\n\u2022 With as few exception types as possible (as determined by catch functionality)<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntemplate&lt;typename DATA_T&gt;\r\nclass OmegaException {\r\npublic:\r\n  OmegaException(std::string str, DATA_T data, const std::source_location&amp; loc = std::source_location::current(), std::stacktrace trace = std::stacktrace::current())\r\n:err_str_{std::move(str)}, user_data_{std::move(data)}, location_{loc}, backtrace_{trace} { }\r\n\r\n  std::string&amp; what() { return err_str_; }\r\n  const std::string&amp; what() const noexcept { return err_str_; }\r\n\r\n  const std::source_location&amp; where() const noexcept { return location_; }\r\n  const std::stacktrace&amp; stack() const noexcept { return backtrace_; }\r\n\r\n  DATA_T&amp; data(){ return user_data_;}\r\n  const DATA_T&amp; data() const noexcept { return user_data_; }\r\n\r\nprivate:\r\n  std::string err_str_;\r\n  DATA_T user_data_;\r\n  const std::source_location location_;\r\n  const std::stacktrace backtrace_;\r\n}\r\n\r\nstd::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const std::source_location&amp; location) {\r\n  os &lt;&lt; location.file_name() &lt;&lt; &quot;(&quot;\r\n  &lt;&lt; location.line() &lt;&lt; &quot;:&quot;\r\n  &lt;&lt; location.column() &lt;&lt; &quot;), function `&quot;\r\n  &lt;&lt; location.function_name() &lt;&lt; &quot;`&quot;;\r\n  return os;\r\n}\r\n\r\nstd::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const std::stacktrace&amp; backtrace) {\r\n  for(auto iter = backtrace.begin(); iter != (backtrace.end()-3); ++iter) {\r\n    os &lt;&lt; iter-&gt;source_file() &lt;&lt; &quot;(&quot; &lt;&lt; iter-&gt;source_line()\r\n   &lt;&lt; &quot;) : &quot; &lt;&lt; iter-&gt;description() &lt;&lt; &quot;\\n&quot;;\r\n  }\r\n  return os;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nenum Errs1 { bad = 1, real_bad };\r\nenum class Errs2 { not_bad = 10, not_good };\r\n\r\nusing MyExceptionErrs1 = OmegaException&lt;Errs1&gt;;\r\nusing MyExceptionErrs2 = OmegaException&lt;Errs2&gt;;\r\n\r\nthrow MyExceptionErrs1(\u201cBad Order id&quot;, real_bad);\r\n\r\ncatch(const MyExceptionErrs1&amp; e) {\r\n  std::cout &lt;&lt; &quot;Failed to process with code (&quot; &lt;&lt; e.data() &lt;&lt; &quot;) : &quot;\r\n  &lt;&lt; e.what() &lt;&lt; \u201c\\n&quot; &lt;&lt; e.where() &lt;&lt; std::endl;\r\n}\r\n\r\n\/*\r\nFailed to process with code (2) : Bad Order id\r\n\/app\/example.cpp(76:69), function `Order findOrder(unsigned int\r\n*\/\r\n\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nstruct bucket {\r\n  int id_;\r\n  Msg msg_;\r\n};\r\n\r\nusing MyExceptionBucket = OmegaException&lt; bucket &gt;;\r\n\r\nthrow MyExceptionBucket (&quot;bad error&quot;, bucket{cliendId, amsg});\r\n\r\ncatch(MyExceptionBucket&amp; eb) {\r\n  std::cout &lt;&lt; &quot;Failed to process id (&quot; &lt;&lt; e.data().id_ &lt;&lt; &quot;) : &quot;\r\n  &lt;&lt; e.what() &lt;&lt; &quot;\\n&quot; &lt;&lt; e.stack() &lt;&lt; std::endl;\r\n  send_error(eb.data().msg_);\r\n}\r\n\r\n\/*\r\nFailed to process id (222) : Bad Order id\r\nexample.cpp(76) : findOrder(unsigned int)\r\nexample.cpp(82) : processOrder(unsigned int)\r\nexample.cpp(97) : main\r\n*\/\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mal ausprobieren ob damit Exception einfacher in der Handhabung werden... Aus Exceptionally Bad: The Misuse of Exceptions in C++ &#038; How to Do Better - Peter Muldoon - CppCon 2023 Conclusion of the talk: What should they used for ? \u2022 Error tracing (logging) \u2022 Stack unwinding (reset \/ termination) \u2022 Data passing \/ Control [&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-5285","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\/5285","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=5285"}],"version-history":[{"count":5,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5285\/revisions"}],"predecessor-version":[{"id":5290,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5285\/revisions\/5290"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5285"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}