Skip to content

Instantly share code, notes, and snippets.

@xxuejie
Created November 21, 2012 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxuejie/4128415 to your computer and use it in GitHub Desktop.
Save xxuejie/4128415 to your computer and use it in GitHub Desktop.
setjmp/longjmp workaround using C++ exceptions
$ 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
$ ~/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
#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