Complete the code to catch errors thrown inside the try block.
try { JSON.parse('invalid json'); } catch ([1]) { console.log('Error caught'); }
The catch block receives the error object as a parameter. Here, e is a common short name for the error.
Complete the code to throw a custom error inside the try block.
try { throw new [1]('Something went wrong'); } catch (e) { console.log(e.message); }
Exception which is not a built-in JavaScript error classError is the base class for errors in JavaScript. Throwing new Error() creates a general error.
Fix the error in the catch block parameter to properly catch the error.
try { JSON.parse('bad'); } catch [1] { console.log('Caught an error'); }
The catch block must have parentheses around the error parameter, like (error).
Fill both blanks to catch a TypeError and log its message.
try { null.[1](); } catch (e) { if (e instanceof [2]) { console.log(e.message); } }
Calling toString on null causes a TypeError. The catch block checks for that error type.
Fill all three blanks to create a try-catch that throws and catches a SyntaxError.
try { throw new [1]('Invalid syntax'); } catch ([2]) { if ([2] instanceof [3]) { console.log([2].message); } }
Error instead of SyntaxErrorThe code throws a SyntaxError, catches it as e, and checks if e is an instance of SyntaxError.