Skip to content

Instantly share code, notes, and snippets.

@b-studios
Created May 21, 2013 18:07
Show Gist options
  • Save b-studios/5621932 to your computer and use it in GitHub Desktop.
Save b-studios/5621932 to your computer and use it in GitHub Desktop.
short excample for cps based exception handling in JavaScript to allow proceedable exception. Uses a global manually maintained stack.
function foo() {
console.log("before exception");
_raise("some exception", function() {
console.log("after exception");
})
}
function bar() {
_try(function() {
foo();
}, function(e, proceed, retry) {
console.log("caught exception");
_raise("catch me if you can!");
}).call(this);
}
/** _raise is global */
var $stack = [{
exec: undefined,
handler: function(exception, _, _) {
throw "uncaught exception " + exception
}
}];
function _raise(exception, cont) {
var stackframe = $stack.pop();
stackframe.handler.call(this, exception, cont, stackframe.exec)
}
function _try(body, handler) {
function execute() {
$stack.push({
exec: execute,
handler: handler
});
body.call(this)
}
function raise(exception, cont) {
handler(exception, cont, execute)
}
return execute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment