{"id":223,"date":"2025-04-26T19:01:15","date_gmt":"2025-04-26T11:01:15","guid":{"rendered":"https:\/\/sy-blog.moe\/?p=223"},"modified":"2025-05-26T10:13:06","modified_gmt":"2025-05-26T02:13:06","slug":"eie111-lecture-11-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86-error-handling","status":"publish","type":"post","link":"https:\/\/sy-blog.moe\/en\/223.html","title":{"rendered":"EIE111 Lecture 11 Error Handling"},"content":{"rendered":"<h1 class=\"wp-block-heading\">Table of contents<\/h1>\n\n\n<ul class=\"lcp_catlist\" id=\"lcp_instance_0\"><li><a href=\"https:\/\/sy-blog.moe\/en\/263.html\">EIE111 Lecture 2 Pointer and Structure<\/a><\/li><li class=\"current\"><a href=\"https:\/\/sy-blog.moe\/en\/223.html\">EIE111 Lecture 11 Error Handling<\/a><\/li><\/ul>\n\n\n\n<p>In std, you can use cerr<sup class=\"iro-term-annotation\" data-term=\"cerr\" data-id=\"1\">(1)<\/sup>Printing exception<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\"><code>std::cin &gt;&gt; a;\nif(a == 0){\n    std::cerr &lt;&lt; \"division by zero!\" &lt;&lt; std::endl;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Program Termination<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. return<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">2. exit()<\/h3>\n\n\n\n<p>Using exit()<sup class=\"iro-term-annotation\" data-term=\"exit()\" data-id=\"2\">(2)<\/sup>Function exits the program, exit(0)<sup class=\"iro-term-annotation\" data-term=\"exit(0)\" data-id=\"3\">(3)<\/sup>It means that the program runs successfully and exits normally.<\/p>\n\n\n\n<pre class=\"wp-block-code cpp\" data-no-translation=\"\"><code>int main() {\n     int a;\n     cin &gt;&gt; a;\n     if (a == 0) {\n         cerr &lt;&lt; \"division by zero!\" &lt;&lt; endl;\n         exit(-1);\n     }\n     cout &lt;&lt; 10 \/ a &lt;&lt; endl;\n     return 0;\n }<\/code><\/pre>\n\n\n\n<p>and return<sup class=\"iro-term-annotation\" data-term=\"return\" data-id=\"4\">(4)<\/sup>The difference is: return is similar to keywords such as continue and break, which are used to return function values; while exit is to exit the program directly. In the main function, there is basically no difference between the two, and both can be used to end the program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. abort()<\/h3>\n\n\n\n<p>abort()<sup class=\"iro-term-annotation\" data-term=\"abort()\" data-id=\"5\">(5)<\/sup>Function can also be used to exit the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Difference between abort() and exit()<\/h2>\n\n\n\n<pre class=\"wp-block-code cpp\" data-no-translation=\"\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Function{\npublic:\n    Function(){\n        cout &lt;&lt; \"Function constructor called\" &lt;&lt; endl;\n    }\n    ~Function(){\n        cout &lt;&lt; \"Function destructor called\" &lt;&lt; endl;\n    }\n};\n\nint main(){\n    Function f1;\n    exit(0);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output: <br>Function constructor<sup class=\"iro-term-annotation\" data-term=\"Function constructor\" data-id=\"6\">(6)<\/sup> called<\/p>\n\n\n\n<pre class=\"wp-block-code cpp\" data-no-translation=\"\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Function{\npublic:\n    Function(){\n        cout &lt;&lt; \"Function constructor called\" &lt;&lt; endl;\n    }\n    ~Function(){\n        cout &lt;&lt; \"Function destructor called\" &lt;&lt; endl;\n    }\n};\nFunction f1;\nint main(){\n    exit(0);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output:<br>Function constructor called<br>Function destructor<sup class=\"iro-term-annotation\" data-term=\"Function destructor\" data-id=\"7\">(7)<\/sup> called<\/p>\n\n\n\n<p>It can be seen that the exit() function can call the destructor, but the automatic object<sup class=\"iro-term-annotation\" data-term=\"\u81ea\u52a8\u5bf9\u8c61\" data-id=\"8\">(8)<\/sup>The destructor of the object will not be called. (Automatic object: an object allocated in a subroutine or block, see<a href=\"https:\/\/www.ibm.com\/docs\/en\/xl-fortran-linux\/16.1.0?topic=objects-automatic\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/www.ibm.com\/docs\/en\/xl-fortran-linux\/16.1.0?topic=objects-automatic<\/a>\uff09<\/p>\n\n\n\n<p>The abort() function does not call the destructor at all.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Assertion<\/h2>\n\n\n\n<p>Requires #include<sup class=\"iro-term-annotation\" data-term=\"#include &lt;cassert&gt;\" data-id=\"9\">(9)<\/sup><\/p>\n\n\n\n<p>usage:<\/p>\n\n\n\n<pre class=\"wp-block-code cpp\" data-no-translation=\"\"><code>#include&lt;iostream&gt;\n#include&lt;cassert&gt;\nusing namespace std;\nint main() {\n    assert(1 == 0);\n    return 0;\n}\nPS D:\\2023-2024\\course-oop\\test&gt; .\\test.exe\nAssertion failed: 1 == 0, file check.cpp, line 5<\/code><\/pre>\n\n\n\n<p>If the assertion fails, the program stops immediately.<\/p>\n\n\n\n<p>Assertions can be ignored using #define NDEBUG.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Exceptions and Errors<\/h2>\n\n\n\n<p>Exception handling in C++ includes: try<sup class=\"iro-term-annotation\" data-term=\"try\" data-id=\"10\">(10)<\/sup>, throw<sup class=\"iro-term-annotation\" data-term=\"throw\" data-id=\"11\">(11)<\/sup>, catch<sup class=\"iro-term-annotation\" data-term=\"catch\" data-id=\"12\">(12)<\/sup><\/p>\n\n\n\n<p>You can use try to define a block of code that you can test for errors when it is executed.<\/p>\n\n\n\n<p>usage:<\/p>\n\n\n\n<pre class=\"wp-block-code cpp\" data-no-translation=\"\"><code>#include &lt;iostream&gt;\nint main(){\n    std::string str(\"foo\");\n    try{\n        str.at(10); \/\/ \u629b\u51fa out_of_range \u5f02\u5e38\n    }catch(const std::out_of_range&amp; e){\n        std::cout &lt;&lt; \"out_of_range: \" &lt;&lt; e.what() &lt;&lt; std::endl; \/\/ what() \u8fd4\u56de\u5f02\u5e38\u7684\u63cf\u8ff0\u4fe1\u606f\n        \/\/ \u8fd9\u91cc\u6355\u83b7\u7684\u662f std::exception \u7684\u5b50\u7c7b std::out_of_range \u5f02\u5e38\n    }catch(const std::exception&amp; e){ \/\/ \u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u6355\u83b7\u5176\u4ed6\u5f02\u5e38\n        std::cout &lt;&lt; \"exception: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Real-world use cases:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"520\" src=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745663053-image-1024x520.png\" alt=\"\" class=\"wp-image-247\" srcset=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745663053-image-1024x520.png 1024w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745663053-image-300x152.png 300w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745663053-image-768x390.png 768w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745663053-image.png 1232w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here, we define a time class and need to ensure that the hours and minutes are in the correct format. So when the time input is not compliant, an exception can be thrown and caught elsewhere.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"527\" src=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745664683-image-1024x527.png\" alt=\"\" class=\"wp-image-250\" srcset=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745664683-image-1024x527.png 1024w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745664683-image-300x154.png 300w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745664683-image-768x395.png 768w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2025\/04\/1745664683-image.png 1160w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Catch has one parameter. When the parameter in catch is the same as the parameter thrown by throw, the content in the catch code block will be executed.<\/p>\n\n\n\n<p>You can use multiple catch to catch different exceptions<\/p>\n\n\n\n<pre class=\"wp-block-code cpp\" data-no-translation=\"\"><code>int main(){\n    try{\n\n    }catch(type1&amp; e1){\n\n    }catch(type2&amp; e2){\n\n    }catch(...){\n        \/\/\u5982\u679c\u4e0d\u6e05\u695a\u9519\u8bef\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528catch(...)\u4ee3\u66ff\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>For details, please see:<a href=\"https:\/\/www.w3schools.com\/cpp\/cpp_exceptions.asp\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/www.w3schools.com\/cpp\/cpp_exceptions.asp<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>return, exit, abort, assertion, exception<\/p>","protected":false},"author":1,"featured_media":259,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[24],"tags":[25],"class_list":["post-223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-notes"],"_links":{"self":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":29,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/223\/revisions\/299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/media\/259"}],"wp:attachment":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}