Try, Catch, Finally

Easy

Try...catch...finally handles errors gracefully. Catch receives error object. Finally always executes for cleanup.

Interactive Visualization

Try/Catch/Finally Flow

try
error
catch
finally
Code executes normally

Key Points

  • try: code that might throw
  • catch: handles errors
  • finally: always runs
  • Error object properties
  • Can re-throw

Code Examples

Try/Catch Pattern

try {
  riskyOperation();
} catch (error) {
  console.error(error.message);
} finally {
  // cleanup always runs
  closeConnection();
}

Handle errors gracefully with guaranteed cleanup