Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
27
Task/Exceptions/C/exceptions-1.c
Normal file
27
Task/Exceptions/C/exceptions-1.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#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 */
|
||||
/* note: if this "default" section is not included, that would be equivalent to a blank "default" section */
|
||||
/* i.e. any exception not caught above would be caught and ignored */
|
||||
/* there is no way to "let the exception through" */
|
||||
}
|
||||
}
|
||||
82
Task/Exceptions/C/exceptions-2.c
Normal file
82
Task/Exceptions/C/exceptions-2.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum exceptions {
|
||||
EXCEPTION_1 = 1,
|
||||
EXCEPTION_2,
|
||||
EXCEPTION_3
|
||||
};
|
||||
|
||||
|
||||
#define throw(exception) do { \
|
||||
if (exp) \
|
||||
longjmp(*exp, exception); \
|
||||
printf("uncaught exception %d\n", exception); \
|
||||
exit(exception); \
|
||||
} while (0)
|
||||
|
||||
#define try(block, catch_block) \
|
||||
{ \
|
||||
jmp_buf *exception_outer = exp; \
|
||||
jmp_buf exception_inner; \
|
||||
exp = &exception_inner; \
|
||||
int exception = setjmp(*exp); \
|
||||
if (!exception) { \
|
||||
do block while(0); \
|
||||
exp = exception_outer; \
|
||||
} else { \
|
||||
exp = exception_outer; \
|
||||
switch(exception) { \
|
||||
catch_block \
|
||||
default: \
|
||||
throw(exception); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define catch(exception, block) \
|
||||
case exception: do block while (0); break;
|
||||
|
||||
|
||||
#define throws jmp_buf* exp
|
||||
|
||||
// define a throwing function
|
||||
void g(throws) {
|
||||
printf("g !\n");
|
||||
throw(EXCEPTION_1);
|
||||
printf("shouldnt b here\n");
|
||||
}
|
||||
|
||||
void h(throws, int a)
|
||||
{
|
||||
printf("h %d!\n", a);
|
||||
throw(EXCEPTION_2);
|
||||
}
|
||||
|
||||
void f(throws) {
|
||||
try({
|
||||
g(exp); // call g with intention to catch exceptions
|
||||
},
|
||||
catch(EXCEPTION_1, {
|
||||
printf("exception 1\n");
|
||||
h(exp, 50); // will throw exception 2 inside this catch block
|
||||
}))
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
throws = NULL; // define exception stack base
|
||||
try({
|
||||
f(exp);
|
||||
},
|
||||
catch(EXCEPTION_2, {
|
||||
printf("exception 2\n");
|
||||
})
|
||||
catch(EXCEPTION_3, {
|
||||
printf("exception 3\n");
|
||||
}))
|
||||
|
||||
h(exp, 60); // will result in "uncaught exception"
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue