Created
May 21, 2013 18:07
-
-
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.
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
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