Created
November 21, 2012 23:02
-
-
Save xxuejie/4128415 to your computer and use it in GitHub Desktop.
setjmp/longjmp workaround using C++ exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ clang++ stack-manipulation-exception.cc | |
$ ./a.out | |
Entering stack_manipulate_func, level: 0 | |
Normal execution path, level: 0 | |
Entering stack_manipulate_func, level: 1 | |
Throws exception at level 1 | |
Error execution path, level: 0 | |
Exiting stack_manipulate_func, level: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ~/develop/mruby-browser/modules/emscripten/em++ -s EXCEPTION_DEBUG=0 stack-manipulation-exception.cc | |
$ node ./a.out.js | |
Entering stack_manipulate_func, level: 0 | |
Normal execution path, level: 0 | |
Entering stack_manipulate_func, level: 1 | |
Throws exception at level 1 | |
Error execution path, level: 0 | |
Exiting stack_manipulate_func, level: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void stack_manipulate_func(int s, int level) { | |
printf("Entering stack_manipulate_func, level: %d\n", level); | |
if (level == 0) { | |
try { | |
printf("Normal execution path, level: %d\n", level); | |
stack_manipulate_func(s, level + 1); | |
} catch (int e) { | |
printf("Error execution path, level: %d\n", level); | |
} | |
} else { | |
printf("Throws exception at level %d\n", level); | |
throw 1; | |
} | |
printf("Exiting stack_manipulate_func, level: %d\n", level); | |
} | |
int main(int argc, char *argv[]) { | |
stack_manipulate_func(42, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment