{"id":1937,"date":"2025-01-26T20:17:19","date_gmt":"2025-01-26T20:17:19","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1937"},"modified":"2025-01-27T18:28:46","modified_gmt":"2025-01-27T18:28:46","slug":"whats-the-problem","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/exceptions-2\/whats-the-problem\/","title":{"rendered":"What&#8217;s The Problem?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_the_Problem\"><\/span>What is the Problem?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Classes have their public interfaces, which traditionally have simply specified what happens under normal circumstances.<\/p>\n\n\n\n<p>But it\u2019s important to define how things work under <em>exceptional<\/em> conditions, as well.<\/p>\n\n\n\n<p>Traditionally, C programmers have used a large set of inconsistent approaches towards indicating success or failure. Often, by returning special values from a function call &#8211; but the types of values vary all over the place! <\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"FILE *fp = fopen(&quot;myfile.dat&quot;, &quot;r&quot;);\nif (fp == NULL)\n  fprintf(stderr, &quot;Failed to open file\\n&quot;);\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #000000\">FILE *fp = <\/span><span style=\"color: #795E26\">fopen<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;myfile.dat&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;r&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (fp == <\/span><span style=\"color: #0000FF\">NULL<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">fprintf<\/span><span style=\"color: #000000\">(stderr, <\/span><span style=\"color: #A31515\">&quot;Failed to open file<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>or:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"int ch = getchar();\nif (ch == EOF)\n  exit(0);\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> ch = <\/span><span style=\"color: #795E26\">getchar<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (ch == EOF)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">exit<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">);<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>and so on&#8230;<\/p>\n\n\n\n<p>But this approach has problems:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It&#8217;s difficult to find an appropriate return code value (<strong><code>EOF<\/code><\/strong>, <strong><code>NULL<\/code><\/strong>, <strong><code>-1<\/code><\/strong>,<strong> <code>0<\/code><\/strong>, \u2026 ) for a function (e.g. <strong><code>atoi()<\/code><\/strong>)<\/li>\n\n\n\n<li>Can produce problems with types (e.g. <strong><code>getchar()<\/code><\/strong>)<\/li>\n\n\n\n<li>A single return code needs to be augmented (e.g. <strong><code>errno<\/code><\/strong>)<\/li>\n\n\n\n<li>Checking for exceptional conditions tends to obscure the algorithm for the normal case, and is error-prone.<\/li>\n\n\n\n<li>Sometimes the code for error checking consumes more space than the normal code.<\/li>\n\n\n\n<li><em><strong>Most Important<\/strong><\/em>:&nbsp; It&#8217;s much too easy for programmers (even well-intentioned ones) to &#8216;forget&#8217; to check the return code.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Exceptions_A_Solution\"><\/span>Exceptions: A Solution<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>For these reasons, some languages, such as Ada, have a feature called <em><strong>exception handling<\/strong><\/em>:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"declare\n  LOW_FLUID_LEVEL : exception\nbegin\n  RUN_ENGINE; -- a procedure call\nexception\n  when LOW_FLUID_LEVEL =&gt;\n    OPEN_VALVE;\n    SOUND_ALARM;\n  when NNUMERIC_ERROR =&gt;\n    CLOSE_VALVE;\n    raise;\n  when others =&gt;\n    LOG_UNKNOWN_ERROR;\nend;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">declare<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  LOW_FLUID_LEVEL : <\/span><span style=\"color: #0000FF\">exception<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">begin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  RUN_ENGINE; <\/span><span style=\"color: #008000\">-- a procedure call<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">exception<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> LOW_FLUID_LEVEL =&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    OPEN_VALVE;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    SOUND_ALARM;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> NNUMERIC_ERROR =&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    CLOSE_VALVE;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">raise<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">others<\/span><span style=\"color: #000000\"> =&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    LOG_UNKNOWN_ERROR;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">end<\/span><span style=\"color: #000000\">;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Exceptions_are_not_necessarily_errors\"><\/span>Exceptions are not necessarily errors<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Note that exceptions are not necessarily errors \u2013 they are exceptional or unusual conditions that are not handled in the normal in-line code.<\/p>\n\n\n\n<p>In Ada, exceptions are a built-in type:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>an exception must be <em><strong>declared<\/strong><\/em> before it may be used (some are predeclared, built into the compiler)<\/li>\n\n\n\n<li>an exception may be <em><strong>raised<\/strong><\/em> (explicitly or implicitly)<\/li>\n\n\n\n<li>an exception may be <em><strong>handled<\/strong><\/em> by one or more <em><strong>exception handlers<\/strong><\/em><\/li>\n\n\n\n<li>An exception has <em><strong>scope<\/strong><\/em>, like any other declaration<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nested_Exception_Handling\"><\/span>Nested Exception Handling<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Exceptions may be raised in inner scopes, and handled in outer scopes.<\/p>\n\n\n\n<p>Here, in nested blocks (<em>again, in Ada<\/em>):<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"procedure MAIN is\nbegin\n  ...\n  declare\n    LOCAL_ERROR : exception; -- exception is a type\n  begin\n    ...\n  exception    -- an exception block\n    when LOCAL_ERROR =&gt;\n      DO_SOMETHING;\n  end;\n  ...\nexception -- another exception block\n  when CONSTRAINT_ERROR =&gt;\n    DO_SOMETHING_ELSE;\n  when NUMERIC_ERROR =&gt;\n    DO_SOMETHING_MORE;\nend MAIN;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">procedure<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">MAIN<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">is<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">begin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">declare<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    LOCAL_ERROR : <\/span><span style=\"color: #0000FF\">exception<\/span><span style=\"color: #000000\">; <\/span><span style=\"color: #008000\">-- exception is a type<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">begin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">exception<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\">-- an exception block<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    when LOCAL_ERROR =&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      DO_SOMETHING;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">end<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">exception<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008000\">-- another exception block<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> CONSTRAINT_ERROR <\/span><span style=\"color: #0000FF\">=&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">DO_SOMETHING_ELSE<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> NUMERIC_ERROR <\/span><span style=\"color: #0000FF\">=&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">DO_SOMETHING_MORE<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">end<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">MAIN<\/span><span style=\"color: #000000\">;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong><code>LOCAL_ERROR<\/code><\/strong> is handled by the exception handler in the inner block, but built-in exceptions <strong><code>CONSTRAINT_ERROR<\/code><\/strong> and <strong><code>NUMERIC_ERROR<\/code><\/strong> are handled in the outer block.<\/p>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Exceptions_in_Nested_Functions\"><\/span>Exceptions in Nested Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here, in nested functions (Ada, again):<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"procedure MAIN is\n  ...\n  type SMALL is digits 5 range 0.0..10.0;\n  function INVERSE(I : float) return SMALL is\n  begin\n    return SMALL(1.0\/I);\n  exception\n    when NUMERIC_ERROR =&gt;\n      return 10.0;\n  end INVERSE;\n  ...\nbegin\n  ...\n  Y := INVERSE(X);\n  ...\nexception\n  when CONSTRAINT_ERROR =&gt;\n    DO_SOMETHING;\nend MAIN;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">procedure<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">MAIN<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">is<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">type<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">SMALL<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">is<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">digits<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">range<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #0000FF\">..<\/span><span style=\"color: #098658\">10.0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">INVERSE<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">I<\/span><span style=\"color: #000000\"> : <\/span><span style=\"color: #267F99\">float<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000FF\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">SMALL<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">is<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">begin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> SMALL(<\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">\/I);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">exception<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> NUMERIC_ERROR <\/span><span style=\"color: #0000FF\">=&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">10.0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">end<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">INVERSE<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">begin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">Y<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">:=<\/span><span style=\"color: #000000\"> INVERSE(X);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">exception<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">when<\/span><span style=\"color: #000000\"> CONSTRAINT_ERROR <\/span><span style=\"color: #0000FF\">=&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">DO_SOMETHING<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">end<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">MAIN<\/span><span style=\"color: #000000\">;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:40%\">\n<p>If we call <strong><code>INVERSE(0.0)<\/code><\/strong>, the division by zero raises an exception <strong><code>NUMERIC_ERROR<\/code><\/strong>, which is handled locally and converted to a return value of <strong><code>10.0<\/code><\/strong><\/p>\n\n\n\n<p>If we call <strong><code>INVERSE(0.0001)<\/code><\/strong>, the resulting value is not within the range of a <strong><code>SMALL<\/code><\/strong>, and when we attempt to return it, a <strong><code>CONSTRAINT_ERROR<\/code><\/strong> is raised, which is handled outside the function.<\/p>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Exception_Handling_Choices\"><\/span>Exception Handling Choices<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>With exceptions, you have the following choices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Handle the exception<\/li>\n\n\n\n<li>Don&#8217;t handle it \u2013 in this case, the program will exit abnormally, when the exception handling facility fails to find an exception handler for the exception.<\/li>\n<\/ul>\n\n\n\n<p>When you decide to <em><strong>handle the exception<\/strong><\/em>, you have the following choices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Abandon the program (last resort \u2013 not user-friendly!)<\/li>\n\n\n\n<li>Try the operation again (e.g. when interacting with a user)<\/li>\n\n\n\n<li>Use an alternative approach (e.g. have extra redundancy in the program to handle such conditions)<\/li>\n\n\n\n<li>Repair the cause of the error and try again.<\/li>\n<\/ul>\n\n\n\n<p><em>Without exceptions<\/em>, you can ignore a problem (intentionally, or otherwise), but it&#8217;s not a good idea.<\/p>\n\n\n\n<p><em>With exceptions<\/em>, it&#8217;s not so easy to just ignore the problem.&nbsp; If you really want to ignore an exception you have to do it explicitly. (Not only is this good practice, it helps the programmer keep track of things.)<\/p>\n\n\n\n<div style=\"height:54px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/exceptions-2\/a-c-not-c-exception-mechanism\/\">Next: A C (not C++) Exception Mechanism <\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Problem? Classes have their public interfaces, which traditionally have simply specified what happens under normal circumstances. But it\u2019s important to define how things work under exceptional conditions, as well. Traditionally, C programmers have used a large set of inconsistent approaches towards indicating success or failure. Often, by returning special values from a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1928,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1937","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=1937"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1937\/revisions"}],"predecessor-version":[{"id":2012,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1937\/revisions\/2012"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1928"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=1937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}