theory.ts

Error Handling & Debugging

Error Handling (try/catch/finally)

The try/catch/finally structure in TypeScript allows handling runtime errors with type safety.

  • The `try` block contains code that may throw an error.
  • The `catch` block handles the error and prevents crashes.
  • The `finally` block executes regardless of whether an error occurred.
// Basic try/catch/finally example
try {
  console.log("Trying something...");
} catch (error) {
  console.error("Error occurred:", error);
} finally {
  console.log("Cleanup code runs here");
}