Created
May 21, 2013 17:28
-
-
Save b-studios/5621601 to your computer and use it in GitHub Desktop.
short example for cps based exception handling without propagation
This file contains hidden or 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
/* | |
* 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