RosettaCodeData/Task/Exceptions-Catch-an-exception-thrown-in-a-nested-call/JavaScript/exceptions-catch-an-exception-thrown-in-a-nested-call.js

36 lines
637 B
JavaScript
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
function U() {}
U.prototype.toString = function(){return this.className;}
function U0() {
this.className = arguments.callee.name;
}
U0.prototype = new U();
function U1() {
this.className = arguments.callee.name;
}
U1.prototype = new U();
function foo() {
for (var i = 1; i <= 2; i++) {
try {
bar();
}
catch(e if e instanceof U0) {
print("caught exception " + e);
}
}
}
function bar() {
baz();
}
function baz() {
// during the first call, redefine the function for subsequent calls
baz = function() {throw(new U1());}
throw(new U0());
}
foo();