LearnFrenzy
Sign in Take a free test

Error Handling · Exercise 3 of 4

Error Handling Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen error-handling programs: SyntaxErrors from a lone try, two catch parameters and a line break after throw; runtime errors from custom errors without super, classes called without new, block-scoped variables, buggy catch and finally blocks; and silent bugs from swallowed errors, missing names, wrong instanceof order, returned errors, lost causes and cleanup outside finally.

Question 11 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
class ValidationError extends Error {
  constructor(message, field) {
    super(message);
    this.field = field;
  }
}
const e = new ValidationError("Email is required", "email");
console.log(String(e), "|", e.name);
A super must be called after this.field
B field should be passed to super
C name is inherited from Error.prototype – set this.name = "ValidationError" in the constructor
D String() cannot convert custom errors
Question page & discussion
Question 12 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
class ValidationError extends Error {
  constructor(m) { super(m); this.name = "ValidationError"; }
}
function message(e) {
  if (e instanceof Error) return "Something went wrong";
  if (e instanceof ValidationError) return `Please fix: ${e.message}`;
  return "Unknown problem";
}
console.log(message(new ValidationError("phone must be 10 digits")));
A instanceof does not work with classes
B ValidationError needs a status code
C Every ValidationError is also an Error, so the general check matches first – test the specific class before Error
D message must be async
Question page & discussion
Question 13 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function divide(a, b) {
  if (b === 0) return new Error("Division by zero");
  return a / b;
}
const perStudent = divide(500, 0);
console.log("Each student gets: " + perStudent);
A Division by zero throws automatically
B b === 0 should be b == 0
C The error is returned like a normal value instead of thrown, so the caller uses it as a result – throw it (and catch it where it can be handled)
D new Error needs a code
Question page & discussion
Question 14 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function loadQuiz(json) {
  try {
    return JSON.parse(json);
  } catch (e) {
    throw new Error("Could not load the quiz");
  }
}
try {
  loadQuiz("{'q': 1}");
} catch (e) {
  console.log(e.message, "| cause:", e.cause?.name);
}
A JSON.parse accepts single quotes
B Optional chaining hides the cause
C catch must re-throw e directly
D The new error discards the original SyntaxError – pass it along: new Error("Could not load the quiz", { cause: e })
Question page & discussion
Question 15 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const ui = { loading: false };
function submitAnswer(answer) {
  ui.loading = true;
  try {
    if (!answer) throw new Error("No option selected");
    console.log("submitted", answer);
    ui.loading = false;
  } catch (e) {
    console.log("error:", e.message);
  }
}
submitAnswer("");
console.log("loading:", ui.loading);
A ui.loading = false runs only on success – move it into a finally block so it runs after errors too
B ui must be declared with let
C The catch block must re-throw
D An empty string is a valid answer
Question page & discussion
Up next · Exercise 4Error Handling Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links