Try catch blocks, custom error types, and common debugging techniques in JavaScript, each explained with a short example.
29 cards · basic cards · AI-written, checked twice. Edit anything.
- What is the syntax for a try-catch block?
Show answer
try { code that might fail } catch (error) { handle error }
- What happens if an error is thrown inside try without a catch?
Show answer
The script stops, the error propagates up, and execution halts unless caught higher up
- Name three properties available on an Error object
Show answer
message, name, stack
- What error does accessing an undefined variable throw?
Show answer
ReferenceError
- What error does calling a non-function throw?
Show answer
TypeError
- What error is thrown for invalid JSON syntax?
Show answer
SyntaxError
- What is the purpose of the finally block?
Show answer
Code that runs after try or catch, regardless of whether an error occurred
- How do you manually throw an error?
Show answer
Use the throw keyword: throw new Error('message') or throw someValue
- How do you create a custom error type?
Show answer
Extend the Error class: class MyError extends Error { constructor(message) { super(message) } }
- What does the stack property of an Error contain?
Show answer
A string showing the call stack at the point the error was thrown, useful for debugging
- What is console.log primarily used for?
Show answer
Printing general information to the console for debugging
- What is the difference between console.error and console.warn?
Show answer
console.error is for errors (red), console.warn is for warnings (yellow or orange)
- What does the debugger statement do?
Show answer
Pauses code execution at that line when developer tools are open, allowing step-through inspection
- How do you catch errors from a Promise?
Show answer
Use .catch() method: promise.catch(error => { handle error })
- How do you use try-catch with async/await?
Show answer
try { const result = await someAsyncFunction() } catch (error) { handle error }