Interview Question

What is try-catch?

try/catch handles thrown failures; finally performs cleanup.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

A try statement runs a block and can handle a thrown completion with catch. • The catch block receives the thrown value through its parameter. • An optional finally block runs after try and catch completion. • Catch only errors that the current code can handle meaningfully.

Example

Code
try {
  JSON.parse('{bad json}');
} catch (error) {
  console.log(error.name);
}
Output
SyntaxError

Quick Revision

try/catch handles thrown failures; finally performs cleanup.