/* Copyright (c) 2000..2009 Michael Stickel This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ /* $Header: /home/doj/a/www/llg/trycatch/try_demo.c,v 1.1 2009-01-06 11:51:51 doj Exp $ */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include "trycatch.h" int exception_demo() { /* You don't have to deliver a real signal to raise an exception. This function shows how to use an arbitrary value to distinguish between events. */ #define EVENT_STRERROR EXCEPTION(1) #define EVENT_INTERROR EXCEPTION(123567) const char* three="three"; int number=-10; TRY(EVENT_STRERROR, EVENT_INTERROR) { int x=atoi(three); if(x != 3) THROW(EVENT_STRERROR); number*=x; if(number<0) THROW(EVENT_INTERROR); } CATCH(EVENT_INTERROR) { printf("your number is less than 0: %i\n", number); } ELSECATCH(EVENT_STRERROR) { printf("your string is not a number: %s\n", three); } return 0; } int function_1(int a) { /* example to show tryreturn() macro to return from a try block */ TRY_ALL_SIGNALS { switch(a) { case 0: a=function_1(++a); tryreturn(a); /* here */ case 1: THROW(SIGUSR1); case 2: /* example to show trybreak to break from a try block */ TRY(SIGILL) { int i=0; while(++i) { if(i==100) trybreak; /* here */ } } CATCH(SIGILL) { return -1; } return 0; } } CATCH_ANY_SIGNAL { /* example to show trycontinue macro to jump out of a try block to the start of an enclosing loop */ int i; for(i=10; i>=0; --i) { TRY(SIGFPE) { if(i>5) trycontinue; /* here */ a+=30/i; } CATCH(SIGFPE) { return function_1(2); } } } return a; } int function_with_try() { TRY(SIGSEGV) { void (*func) () = NULL; printf ("entering crashing function call: envsp=%d\n", __try_env_sp); func(); printf ("leaving crashing function call\n"); } CATCH(SIGSEGV) { printf ("resume from failed function call\n"); return 1; } return 0; } int recursive(int r) { printf("recursive: we have %i\n", r); TRY(SIGUSR1) { if(r>0) { recursive(r-1); THROW(SIGUSR1); } } CATCH(SIGUSR1) { printf("recursive: received at %i\n", r); } printf("recursive: we leave %i\n", r); return r; } int main () { int *xp = NULL; int x=0; srand(time(NULL)); printf ("------------ a very short try-catch ---------------\n"); TRY(SIGUSR1, SIGSEGV) x = *xp; CATCH(SIGSEGV) x = 10; printf ("x=%d\n", x); printf ("------------- a try catch which gives the cause for the crash -------\n"); TRY(SIGPIPE, SIGFPE) { int i, j; THROW(SIGPIPE); i=0; j = 10/i; } CATCH(SIGFPE) { printf("we have a floating point exception\n"); } ELSECATCH(SIGPIPE) { printf("now we have a sigpipe\n"); } printf ("---------- some cascaded try-catch -------------\n"); TRY(SIGSEGV) { printf ("function_with_try() = %d\n", function_with_try()); printf ("entering crashing code\n"); *((char *)0x11111111) = 11; printf ("leaving crashing code\n"); } CATCH(SIGSEGV) { printf ("resume from failed try\n"); TRY(SIGSEGV) { printf ("entering crashing second code\n"); *((char *)0x22222222) = 22; printf ("leaving crashing second code\n"); } CATCH(SIGSEGV) { printf ("resume from failed second try\n"); } } recursive(10); printf ("---------- catch them all -------------\ncaught signal"); for(x=1; x