Return to JUNTO

JUNTO Practice: Eloquent JavaScript, Chapter 8 Exercises

Discussed on April 09, 2020.

https://eloquentjavascript.net/08_error.html

Exercises:

Solutions

Click to see:

Oscar Martinez

"Retry"

class MultiplicatorUnitFailure extends Error {}

function primitiveMultiply(a, b) {
  if (Math.random() < 0.2) {
    return a * b;
  } else {
    throw new MultiplicatorUnitFailure("Klunk");
  }
}

function reliableMultiply(a, b) {
  try {
  return primitiveMultiply(a,b);
  } catch {
  return reliableMultiply(a,b)
  }
}

console.log(reliableMultiply(8, 8));

"The Locked Box"

const box = {
  locked: true,
  unlock() { this.locked = false; },
  lock() { this.locked = true;  },
  _content: [],
  get content() {
    if (this.locked) throw new Error("Locked!");
    return this._content;
  }
};

function withBoxUnlocked(body) {
  // Your code here.
  box.unlock();
  try {
    body()    
  } finally {
box.lock()
  }
}

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);

John Lekberg

"Retry"

class MultiplicatorUnitFailure extends Error {}

function primitiveMultiply(a, b) {
  if (Math.random() < 0.2) {
    return a * b;
  } else {
    throw new MultiplicatorUnitFailure("Klunk");
  }
}

function reliableMultiply(a, b) {
  for (;;) {
    try {
      return primitiveMultiply(a, b);
    } catch (e) {
      if (e instanceof MultiplicatorUnitFailure) {
        // do nothing
      } else {
        throw e;
      }
    }
  }
}

"The Locked Box"

const box = {
  locked: true,
  unlock() { this.locked = false; },
  lock() { this.locked = true;  },
  _content: [],
  get content() {
    if (this.locked) throw new Error("Locked!");
    return this._content;
  }
};

function withBoxUnlocked(body) {
  try {
    box.unlock();
    body();
  } finally {
    box.lock();
  }
}

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);
}

Daniel Bassett

"Retry"

class MultiplicatorUnitFailure extends Error {}

function primitiveMultiply(a, b) {
 if (Math.random() < 0.2) {
   return a * b;
 } else {
   throw new MultiplicatorUnitFailure("Klunk");
 }
}

function reliableMultiply(a, b) {
 try {
   return primitiveMultiply(a, b);
 } catch (e) {
   return reliableMultiply(a, b);
 }
}

console.log(reliableMultiply(8, 8));

"The Locked Box"

const box = {
 locked: true,
 unlock() {
   this.locked = false;
 },
 lock() {
   this.locked = true;
 },
 _content: [],
 get content() {
   if (this.locked) throw new Error("Locked!");
   return this._content;
 }
};

function withBoxUnlocked(body) {
 if (box.locked) {
   box.unlock();
   try {
     return body();
   } finally {
     box.lock();
   }
 }
 return body();
}

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);