{"id":1942,"date":"2025-01-26T20:26:52","date_gmt":"2025-01-26T20:26:52","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1942"},"modified":"2025-01-27T18:32:22","modified_gmt":"2025-01-27T18:32:22","slug":"a-c-not-c-exception-mechanism","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/exceptions-2\/a-c-not-c-exception-mechanism\/","title":{"rendered":"A C (not C++) Exception Mechanism"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_C_not_C_Exception_Mechanism\"><\/span>A C (not C++) Exception Mechanism<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While C does not have exceptions built-in, it is possible to add a reasonably efficient and portable (albeit somewhat crude) exception mechanism using macros and the <strong><code>setjmp<\/code><\/strong>\/<strong><code>longjmp<\/code><\/strong> C Run-Time-Library functions (see <a href=\"https:\/\/wiki.sei.cmu.edu\/confluence\/display\/c\/MSC22-C.+Use+the+setjmp%28%29%2C+longjmp%28%29+facility+securely\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Roberts_Proposal\"><\/span>The Roberts Proposal<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">A very useful C exception mechanism was proposed by Eric S. Roberts in 1989:<br><a href=\"http:\/\/bitsavers.informatik.uni-stuttgart.de\/pdf\/dec\/tech_reports\/SRC-RR-40.pdf\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Implementing Exceptions in C<\/em>, by Eric S. Roberts<\/a><em>, Digital Systems Research Center Report #40, 1989<\/em><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Roberts&#8217; approach allows the following (<em>example in C<\/em>):<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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=\"#include &quot;exception.h&quot;\n  ...\nexception OpenError; \/\/ exception is a type\n  ...\nTRY                 \/\/ TRY is a C macro\n  ...\n  file = OpenFile(&quot;test.dat&quot;, &quot;r&quot;);\n  ...\nEXCEPT(OpenError)  \/\/ EXCEPT and ENDTRY are C macros\n  fprintf(stderr, &quot;Cannot open file 'test.dat'\\n&quot;);\nENDTRY\" 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: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;exception.h&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">exception OpenError;<\/span><span style=\"color: #008000\"> \/\/ exception is a type<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">TRY<\/span><span style=\"color: #008000\">                 \/\/ TRY is a C macro<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  file = <\/span><span style=\"color: #795E26\">OpenFile<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;test.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: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">EXCEPT<\/span><span style=\"color: #000000\">(OpenError)<\/span><span style=\"color: #008000\">  \/\/ EXCEPT and ENDTRY are C macros<\/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;Cannot open file &#39;test.dat&#39;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">ENDTRY<\/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%\"><\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The general form of a <strong><code>TRY<\/code>&#8211;<code>ENDTRY<\/code><\/strong> block is:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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=\"TRY\n  statements\nEXCEPT(exception-name-1)\n  statements\nEXCEPT(exception-name-2)\n  statements\n...\nENDTRY\" 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\">TRY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  statements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">EXCEPT<\/span><span style=\"color: #000000\">(exception-name-<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  statements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">EXCEPT<\/span><span style=\"color: #000000\">(exception-name-<\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  statements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">ENDTRY<\/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 class=\"wp-block-paragraph\">An exception is raised by:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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=\"RAISE(exception-name, value);\" 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: #795E26\">RAISE<\/span><span style=\"color: #000000\">(exception-name, value);<\/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 class=\"wp-block-paragraph\">where the <strong><code>value<\/code><\/strong> is an <strong><code>int<\/code><\/strong> to be communicated back to the handler, to provide additional information about the exception.<\/p>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_TRY-FINALLY_Block_and_Resource_Acquisition\"><\/span>The TRY-FINALLY Block, and Resource Acquisition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This approach also included the option of a <em><strong>finally block<\/strong><\/em> (<em>again, in C<\/em>):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Roberts also specified a <strong><code>TRY<\/code>&#8211;<code>FINALLY<\/code><\/strong> block, which is of the form:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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=\"TRY\n  statements\nFINALLY\n  statements\nENDTRY\" 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\">TRY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  statements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">FINALLY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  statements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">ENDTRY<\/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 class=\"wp-block-paragraph\">The major point of a <code><strong>FINALLY<\/strong><\/code> block is to allow for convenient cleanup in all cases:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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(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=\"#include &quot;exception.h&quot;\n  ...\nAcquire(Resource);\nTRY\n  \/* code for accessing the resource *\/\nFINALLY \/\/ FINALLY is a C macro\n  \/* this is executed whether or not an exception occurs *\/\n  Release(Resource);\nENDTRY\" 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: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;exception.h&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">Acquire<\/span><span style=\"color: #000000\">(Resource);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">TRY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/* code for accessing the resource *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">FINALLY<\/span><span style=\"color: #008000\"> \/\/ FINALLY is a C macro<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/* this is executed whether or not an exception occurs *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Release<\/span><span style=\"color: #000000\">(Resource);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">ENDTRY<\/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 class=\"wp-block-paragraph\">This allows for acquired resources to be released, whether the access is successful or not<\/p>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"setjmp_longjmp_and_jmp_buf\"><\/span><strong>setjmp<\/strong>, <strong>longjmp<\/strong> and <strong>jmp_buf<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Roberts&#8217; exception handling mechanism made use of the <code><strong>setjmp<\/strong><\/code> and <code><strong>longjmp<\/strong><\/code> functions, which use a buffer called <code><strong>jmp_buf<\/strong><\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Function_setjmp\"><\/span>Function <strong><code>setjmp<\/code><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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=\"#include &lt;setjmp.h&gt;\n...\nint setjmp(jmp_buf env);\" 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: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;setjmp.h&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">setjmp<\/span><span style=\"color: #000000\">(jmp_buf <\/span><span style=\"color: #001080\">env<\/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 class=\"wp-block-paragraph\">saves the current &#8216;environment&#8217; in its <strong><code>jmp_buf<\/code><\/strong> argument, and returns <strong><code>0<\/code><\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Function_longjmp\"><\/span>Function <strong><code>longjmp<\/code><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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=\"#include &lt;setjmp.h&gt;\n...\nvoid longjmp(jmp_buf env, int val);\" 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: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;setjmp.h&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">longjmp<\/span><span style=\"color: #000000\">(jmp_buf <\/span><span style=\"color: #001080\">env<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">val<\/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 class=\"wp-block-paragraph\">restores the environment saved by <strong><code>setjmp<\/code><\/strong>, which causes a return from the <strong><code>setjmp<\/code><\/strong> call with a value of <strong><code>val<\/code><\/strong>.  <strong><code>longjmp<\/code><\/strong> never itself returns!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"jmp_buf\"><\/span>jmp_buf<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Type <strong><code>jmp_buf<\/code><\/strong> is a system-specific type that is of sufficient size to save the &#8216;environment&#8217; (such as machine registers, etc.) so that the machine state may be saved, and later restored.&nbsp; (It must be an array type, so that a pointer to it is always passed to a function.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"An_Example\"><\/span>An Example:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-995f960e 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);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  setjmpTest.c\n\/\/  setjmp &amp; longjmp\n\/\/\n\/\/  Created by Bryan Higgs on 9\/23\/24.\n\/\/\n\n#include &lt;stdio.h&gt;\n#include &lt;setjmp.h&gt;\n\njmp_buf main_env;\n\nint InputNumber(void)\n{\n  int n, ret;\n  fflush(stdin);  \/\/ ensure there's nothing left\n                  \/\/ from the last scanf\n  ret = scanf(&quot;%d&quot;, &amp;n);\n                  \/\/ read an integer from stdin\n  if (ret &lt;= 0)\n  {\n    \/\/ something went wrong during the read...\n    printf(&quot;Return value from scanf = %d &quot;, ret);\n    longjmp(main_env, 1);\n  }\n  return n;\n}\n\nint main()\n{\n  int n;\n  if (setjmp(main_env) != 0)\n    printf(&quot;\\nInvalid, try again...\\n&quot;);\n  printf(&quot;Enter a valid number:&quot;);\n  n = InputNumber();\n  printf(&quot;You entered the number: %d\\n&quot;, n);\n  return 0;\n}\" 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: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  setjmpTest.c<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  setjmp &amp; longjmp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 9\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;stdio.h&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;setjmp.h&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">jmp_buf main_env;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">InputNumber<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">)<\/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\">int<\/span><span style=\"color: #000000\"> n, ret;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">fflush<\/span><span style=\"color: #000000\">(stdin);<\/span><span style=\"color: #008000\">  \/\/ ensure there&#39;s nothing left<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                  <\/span><span style=\"color: #008000\">\/\/ from the last scanf<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ret = <\/span><span style=\"color: #795E26\">scanf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #001080\">%d<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">, &amp;n);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                  <\/span><span style=\"color: #008000\">\/\/ read an integer from stdin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (ret &lt;= <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\">\/\/ something went wrong during the read...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Return value from scanf = <\/span><span style=\"color: #001080\">%d<\/span><span style=\"color: #A31515\"> &quot;<\/span><span style=\"color: #000000\">, ret);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">longjmp<\/span><span style=\"color: #000000\">(main_env, <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> n;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/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\">int<\/span><span style=\"color: #000000\"> n;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #795E26\">setjmp<\/span><span style=\"color: #000000\">(main_env) != <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">Invalid, try again...<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Enter a valid number:&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  n = <\/span><span style=\"color: #795E26\">InputNumber<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;You entered the number: <\/span><span style=\"color: #001080\">%d<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">, n);<\/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\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><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 class=\"wp-block-paragraph\">Here&#8217;s the user&#8217;s (my) interaction with the program:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"line-height:1.4\"><code>Enter a valid number:<kbd>x<\/kbd>\nReturn value from scanf = 0<kbd> <\/kbd>\nInvalid, try again...\nEnter a valid number:<kbd>52<\/kbd>\nYou entered the number: 52\nProgram ended with exit code: 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>See if you can figure out how the program works!<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that <strong><code>longjmp<\/code><\/strong> does not clean up any resources that may have been allocated since the <strong><code>setjmp<\/code><\/strong>.&nbsp; <em>That is the programmer&#8217;s responsibility!<\/em><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:clamp(14.082px, 0.88rem + ((1vw - 3.2px) * 0.54), 21px);\">Well, enough already of Ada and C implementations. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:clamp(14.082px, 0.88rem + ((1vw - 3.2px) * 0.54), 21px);\">Let&#8217;s see what C++ implements&#8230;<\/p>\n\n\n\n<div style=\"height:47px\" 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\/c-exceptions\/\">Next: C++ Exceptions<\/a><\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A C (not C++) Exception Mechanism While C does not have exceptions built-in, it is possible to add a reasonably efficient and portable (albeit somewhat crude) exception mechanism using macros and the setjmp\/longjmp C Run-Time-Library functions (see here). The Roberts Proposal A very useful C exception mechanism was proposed by Eric S. Roberts in 1989:Implementing [&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-1942","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1942","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=1942"}],"version-history":[{"count":7,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1942\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1942\/revisions\/2014"}],"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=1942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}