Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The traditional approach is to... well... not to :)</p> <p>Generally, people just check the status after attempting something, and if something went wrong, they clean up and return an error code, rather than continuing. This error checking and returning propagates back down the function call stack until it gets back to somewhere high-level in your program, where you care to inform the user instead of just returning more errors:</p> <pre><code>int try_it() { if (!do_something(...)) { return TRYIT_FAILURE; } } void my_gui() { rc = try_it() if (rc == TRYIT_FAILURE) { message_box("failed when trying it.", MB_ABORT|MB_RETRY); } ... } </code></pre> <p>You can also do this in a big function, using nested ifs, if you want something like a try...except construct:</p> <pre><code>if (stage1()) { if (stage2()) { if(stage3()) { printf("success!\n"); } else { // error handling for stage 3 } } else { // error handling for stage 2 } } else { // error handling for stage 1 } </code></pre> <p>And you can do the same with gotos if you're a bit mad.</p> <p>However, you can do actual exceptions, at least in C. C has two standard library calls for this sort of thing: setjmp and longjmp. With these, you can jump back through several function calls to a predetermined place, where you know the exception (jump) happened. See <a href="http://en.wikipedia.org/wiki/Setjmp.h#Exception_handling" rel="nofollow noreferrer">Setjmp.h#exception_handling on wikipedia</a> for more on that.</p> <p>It seems Perl does have a way of doing this, though it looks far from intuitive to me. Then again, I don't code in Perl :)</p> <p><a href="http://unlser1.unl.csi.cuny.edu/faqs/perl-faq/Q4.8.html" rel="nofollow noreferrer">Perl FAQ Q4.8</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload