Exceptions: Difference between revisions

Content added Content deleted
m (Spelling/grammar/aesthetics)
Line 36: Line 36:
===error===
===error===
error "Error message." number 2000
error "Error message." number 2000

=={{header|C}}==

The setjmp()/longjmp() functions in the C standard library header <setjmp.h> is typically used for exception handling.

===try-catch===

#include <setjmp.h>
enum { MY_EXCEPTION = 1 }; /* any non-zero number */
jmp_buf env;

void foo()
{
longjmp(env, MY_EXCEPTION); /* throw MY_EXCEPTION */
}
void call_foo()
{
switch (setjmp(env)) {
case 0: /* try */
foo();
break;
case MY_EXCEPTION: /* catch MY_EXCEPTION */
/* handle exceptions of type MY_EXCEPTION */
break;
default:
/* handle any type of exception not handled by above catches */
}
}


=={{header|C++}}==
=={{header|C++}}==