Error Handling
Difficulty: medium
Overview
try/catch/finally handles runtime errors. The catch block receives an Error object with name and message properties. Built-in error types: Error, TypeError, ReferenceError, SyntaxError, RangeError, URIError, EvalError. throw can emit any value. In async code, rejected Promises must be caught with .catch() or try/catch inside an async function to prevent unhandled rejection warnings.
Practice Linked Questions
Q1. Which of the following is the correct syntax for a try-catch block?
Select one answer before revealing.
Q2. What is the output? ```js try { throw new TypeError("bad type"); } catch (e) { console.log(e.name, e.message); } ```
Select one answer before revealing.
Q3. Which built-in error type is thrown when accessing a variable that has not been declared?
Select one answer before revealing.