Content deleted Content added
Phil Boswell (talk | contribs) →[[Objective Caml|OCaml]]: <source> |
Phil Boswell (talk | contribs) →[[C programming language|C]]: <source> |
||
Line 217:
The most common way to implement exception handling in standard C
is to use [[setjmp/longjmp]] functions:
<source lang=c>
{
if(/* something happened */)
</source>
Some [[operating system]]s also have similar features, for example [[Microsoft Windows]] has "[[Structured Exception Handling]]" (SEH):▼
▲ #include <setjmp.h>
<source lang=c>
▲ #include <stdio.h>
▲ enum { SOME_EXCEPTION = 1 };
▲ jmp_buf state;
▲ int main()
▲ {
▲ int exception;
▲ if((exception = setjmp(state)) == 0) // try
__try {
▲ longjmp(state, SOME_EXCEPTION); // throw SOME_EXCEPTION
▲ }
▲ else switch(exception)
▲ {
▲ case SOME_EXCEPTION: // catch SOME_EXCEPTION
▲ puts("SOME_EXCEPTION caught");
▲ break;
▲ default: // catch ...
▲ puts("Some strange exception");
}
return 0;
</source>
▲Some [[operating system]]s also have similar features, for example [[Microsoft Windows]] has "[[Structured Exception Handling]]" (SEH):
▲ int filterExpression (EXCEPTION_POINTERS* ep) {
▲ ++ep->ContextRecord->Eip;
▲ return EXCEPTION_CONTINUE_EXECUTION;
▲ }
▲ int main() {
▲ static int zero;
▲ printf ("Past the exception.\n");
▲ } __except (filterExpression (GetExceptionInformation())) {
▲ printf ("Handler called.\n");
▲ }
▲ return 0;
▲ }
* see also [[Vectored Exception Handling]] (VEH).
|