Exceptions/Catch an exception thrown in a nested call: Difference between revisions

(added objective-c)
Line 164:
Return 1
}</lang>
 
=={{header|C}}==
C doesn't have exception handling. But <i>that</i> won't stop crazy people.
<lang C>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ucontext.h>
 
#define try push_handler(); if (!exc_string)
#define catch(e) pop_handler();\
if (exc_string && strcmp(e, exc_string)) {\
if (exc_depth) throw(exc_string);\
else {\
fprintf(stderr, "Unhandled exception %s\n", exc_string);\
exit(1);\
}\
}\
for (; exc_string; exc_string = 0)
 
ucontext_t *exc;
int exc_depth = 0;
int exc_alloc = 0;
char * exc_string;
char * e_saved;
 
void throw(char *str)
{
exc_string = str;
setcontext(exc + exc_depth - 1);
}
 
void push_handler()
{
exc_string = 0;
if (exc_alloc <= exc_depth) {
exc_alloc += 16;
exc = realloc(exc, sizeof(ucontext_t) * exc_alloc);
}
getcontext(exc + exc_depth++);
}
 
void pop_handler()
{
exc_depth --;
}
 
void baz() {
static int count = 0;
switch (count++) {
case 0: throw("U0");
case 1: throw("U1");
case 2: throw("U2");
}
}
 
void foo()
{
printf(" foo: calling baz\n");
try { baz(); }
catch("U0") { printf(" foo: got exception U0; handled and dandy\n"); }
printf(" foo: finished\n");
}
 
int main() {
int i;
for (i = 0; i < 3; i++) {
printf("main: calling foo: %d\n", i);
try { foo(); }
catch("U1") { printf("main: Someone threw U1; handled and dandy\n"); }
}
return 0;
}</lang>Output:<lang>main: calling foo: 0
foo: calling baz
foo: got exception U0; handled and dandy
foo: finished
main: calling foo: 1
foo: calling baz
main: Someone threw U1; handled and dandy
main: calling foo: 2
foo: calling baz
Unhandled exception U2</lang>
Disclaimer: this is pure hackery. You are not seriously going to use it.
 
=={{header|C++}}==
Anonymous user