Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Created October 26, 2014 22:15
Show Gist options
  • Save Shiggiddie/a67bf33ae4580c71078c to your computer and use it in GitHub Desktop.
Save Shiggiddie/a67bf33ae4580c71078c to your computer and use it in GitHub Desktop.
// 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