Try/Catch/Finally in JavaScript

From the JavaScript cheat sheet · Error Handling · verified Jul 2026

Try/Catch/Finally

Handling exceptions with try/catch

javascript
// Basic try/catch
try {
  // code that may throw
  riskyOperation();
} catch (error) {
  console.error(error.message);
} finally {
  // always runs
  cleanup();
}
💡 finally block runs whether error occurs or not
⚡ Catch specific error types with instanceof
📌 Rethrow errors when you can't fully handle them
🟢 See also: Async JavaScript sheet for async error handling with try/catch + await

More JavaScript tasks

Back to the full JavaScript cheat sheet