Created
October 26, 2014 22:15
-
-
Save Shiggiddie/a67bf33ae4580c71078c to your computer and use it in GitHub Desktop.
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
// Retry | |
function MultiplicatorUnitFailure() {} | |
MultiplicatorUnitFailure.prototype = Object.create(Error.prototype); | |
function primitiveMultiply(a, b) { | |
if (Math.random() < 0.5) | |
return a * b; | |
else | |
throw new MultiplicatorUnitFailure(); | |
} | |
function reliableMultiply(a, b) { | |
// Your code here. | |
for (;;) { | |
try { | |
return primitiveMultiply(a,b); | |
} catch (e) { | |
if (e instanceof MultiplicatorUnitFailure) | |
console.log("MultiplicatorUnitFailure is stupid"); | |
else | |
throw e; | |
} | |
} | |
} | |
console.log(reliableMultiply(8, 8)); | |
// The Locked Box | |
function withBoxUnlocked(body) { | |
if (box.locked) { | |
var finFunc = box.lock | |
box.unlock() | |
} | |
else | |
var finFunc = box.unlock | |
try { | |
body() | |
} finally { | |
finFunc(); | |
} | |
} | |
withBoxUnlocked(function() { | |
box.content.push("gold piece"); | |
}); | |
try { | |
withBoxUnlocked(function() { | |
throw new Error("Pirates on the horizon! Abort!"); | |
}); | |
} catch (e) { | |
console.log("Error raised:", e); | |
} | |
console.log(box.locked); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment