Question 12
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
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 Correct
- D message must be async
Correct Answer: Option C
Order checks from most specific to most general.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!