Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using finally for Cleanup | Understanding and Handling Errors
Error Handling in JavaScript

bookUsing finally for Cleanup

The finally block is a crucial part of JavaScript's error handling system. When you use try and catch to handle errors, the finally block allows you to specify code that must run after the try and catch blocks finish. This is especially important for cleanup tasks, such as closing files, releasing network connections, or resetting variables. No matter whether an error is thrown or caught, any code in the finally block will always execute.

This guarantees that resources are released and your program remains stable and efficient, even when unexpected errors occur.

1234567891011121314151617181920
function openConnection() { console.log("Connection opened"); return { close: () => console.log("Connection closed") }; } function doWorkWithConnection() { const connection = openConnection(); try { console.log("Working with connection"); // Simulate an error during work throw new Error("Something went wrong while working!"); } catch (err) { console.log("Caught error:", err.message); } finally { connection.close(); console.log("Cleanup done in finally block"); } } doWorkWithConnection();
copy

In this example, you see how the finally block guarantees that cleanup code runs, whether or not an error occurs. The openConnection function simulates opening a resource, and the doWorkWithConnection function uses a try/catch/finally structure. Inside the try block, an error is thrown on purpose to represent something going wrong while working with the resource. The catch block handles the error, displaying the error message. The finally block then executes the cleanup code—calling connection.close()—no matter what happened before.

question mark

What is the main purpose of the finally block in JavaScript error handling?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 7.69

bookUsing finally for Cleanup

Stryg for at vise menuen

The finally block is a crucial part of JavaScript's error handling system. When you use try and catch to handle errors, the finally block allows you to specify code that must run after the try and catch blocks finish. This is especially important for cleanup tasks, such as closing files, releasing network connections, or resetting variables. No matter whether an error is thrown or caught, any code in the finally block will always execute.

This guarantees that resources are released and your program remains stable and efficient, even when unexpected errors occur.

1234567891011121314151617181920
function openConnection() { console.log("Connection opened"); return { close: () => console.log("Connection closed") }; } function doWorkWithConnection() { const connection = openConnection(); try { console.log("Working with connection"); // Simulate an error during work throw new Error("Something went wrong while working!"); } catch (err) { console.log("Caught error:", err.message); } finally { connection.close(); console.log("Cleanup done in finally block"); } } doWorkWithConnection();
copy

In this example, you see how the finally block guarantees that cleanup code runs, whether or not an error occurs. The openConnection function simulates opening a resource, and the doWorkWithConnection function uses a try/catch/finally structure. Inside the try block, an error is thrown on purpose to represent something going wrong while working with the resource. The catch block handles the error, displaying the error message. The finally block then executes the cleanup code—calling connection.close()—no matter what happened before.

question mark

What is the main purpose of the finally block in JavaScript error handling?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4
some-alt