Quiz

How do you handle errors using `try...catch` blocks?

Topics
JavaScript
Edit on GitHub

TL;DR

To handle errors using try...catch blocks, you wrap the code that might throw an error inside a try block. If an error occurs, the control is transferred to the catch block where you can handle the error. Optionally, you can use a finally block to execute code regardless of whether an error occurred or not.

try {
// Code that may throw an error
} catch (error) {
// Handle the error
} finally {
// Code that will run regardless of an error
}

How do you handle errors using try...catch blocks?

Basic structure

The try...catch statement consists of a try block, a catch block, and optionally a finally block.

try {
// Code that may throw an error
} catch (error) {
// Handle the error
} finally {
// Code that will run regardless of an error
}

Example

Here is an example of using try...catch to handle errors:

try {
let result = riskyOperation();
console.log(result);
} catch (error) {
console.error('An error occurred:', error.message);
} finally {
console.log('This will run regardless of an error');
}

Explanation

  • try block: Contains code that might throw an error. If an error occurs, the control is transferred to the catch block.
  • catch block: Contains code to handle the error. The error object contains information about the error.
  • finally block: Contains code that will run regardless of whether an error occurred or not. This is useful for cleanup tasks.

Nested try...catch blocks

You can nest try...catch blocks to handle different levels of errors:

try {
try {
let result = anotherRiskyOperation();
console.log(result);
} catch (innerError) {
console.error('Inner error:', innerError.message);
}
} catch (outerError) {
console.error('Outer error:', outerError.message);
}

Re-throwing errors

You can re-throw an error from the catch block if you want it to be handled by an outer try...catch block:

try {
try {
let result = yetAnotherRiskyOperation();
console.log(result);
} catch (innerError) {
console.error('Inner error:', innerError.message);
throw innerError; // Re-throw the error
}
} catch (outerError) {
console.error('Outer error:', outerError.message);
}

Using finally for cleanup

The finally block is useful for cleanup tasks, such as closing a file or releasing resources:

try {
let file = openFile('example.txt');
// Perform operations on the file
} catch (error) {
console.error('An error occurred:', error.message);
} finally {
closeFile(file); // Ensure the file is closed
}

Further reading

Edit on GitHub