Skip to content

Instantly share code, notes, and snippets.

@b-studios
Created May 21, 2013 17:28
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 b-studios/5621601 to your computer and use it in GitHub Desktop.
Save b-studios/5621601 to your computer and use it in GitHub Desktop.
short example for cps based exception handling without propagation
/*
* try {
* var foo = 3
* raise "some exception"
* console.log("do some stuff");
* } catch(msg) {
* console.log("caught exception")
* proceed
* }
*
* could easily be compiled into
*/
_try(function(raise) {
var foo = 3;
raise("some exception", function() {
console.log("do some stuff");
});
// catch
},function(e, proceed, retry) {
console.log("caught exception")
proceed()
}).call(this)
// of course doesn't play well with `return`, since it is captured by this encoding as functions
function _try(body, handler) {
function execute() {
body.call(this, raise)
}
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