Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære The finally Block | Handling Exceptions in Java
Exceptions and Error Handling in Java

bookThe finally Block

Note
Definition

The finally block in Java is a crucial part of exception handling that ensures certain code always runs, no matter what happens in the try or catch blocks.

You typically use the finally block for tasks like resource cleanup—closing files, releasing network connections, or freeing up memory—so that your application remains robust and efficient even when errors occur.

try {
    // code
} catch {
    // code
} finally {
    // code
}

The execution of the finally block is special because it runs every time the associated try statement completes, whether an exception is thrown or not. If an exception occurs in the try block and is caught by a catch block, the finally block executes after the catch finishes. If no exception is thrown, the finally block still executes after the try block. Even if the code in the try or catch block contains a return statement, the finally block will execute before the method returns control to the caller. This behavior makes the finally block a reliable place for actions that must always happen, such as closing files or releasing other resources.

Main.java

Main.java

copy
12345678910111213141516171819202122
package com.example; public class Main { public static void main(String[] args) { String[] numbers = {"10", "20", "not-a-number", "40"}; int index = 2; int result = 0; try { System.out.println("Attempting to parse value..."); result = Integer.parseInt(numbers[index]); System.out.println("Parsed value: " + result); } catch (NumberFormatException e) { System.out.println("Parsing failed: " + e.getMessage()); } finally { // This block always executes System.out.println("The 'finally' block has executed — cleanup or final steps go here."); } System.out.println("Program continues..."); } }

The finally block in Java is a special block that always executes after a try block, regardless of whether an exception was thrown or caught. It is used to ensure that important final actions, like closing resources or performing cleanup, are always performed.

In the example above, the program attempts to parse a value from an array. Even if parsing fails and the catch block executes, the finally block still runs, guaranteeing that the final message is printed and any necessary cleanup occurs. This makes the program more reliable and prevents resource leaks.

question mark

What is guaranteed about code placed inside a finally block in Java?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 10

bookThe finally Block

Sveip for å vise menyen

Note
Definition

The finally block in Java is a crucial part of exception handling that ensures certain code always runs, no matter what happens in the try or catch blocks.

You typically use the finally block for tasks like resource cleanup—closing files, releasing network connections, or freeing up memory—so that your application remains robust and efficient even when errors occur.

try {
    // code
} catch {
    // code
} finally {
    // code
}

The execution of the finally block is special because it runs every time the associated try statement completes, whether an exception is thrown or not. If an exception occurs in the try block and is caught by a catch block, the finally block executes after the catch finishes. If no exception is thrown, the finally block still executes after the try block. Even if the code in the try or catch block contains a return statement, the finally block will execute before the method returns control to the caller. This behavior makes the finally block a reliable place for actions that must always happen, such as closing files or releasing other resources.

Main.java

Main.java

copy
12345678910111213141516171819202122
package com.example; public class Main { public static void main(String[] args) { String[] numbers = {"10", "20", "not-a-number", "40"}; int index = 2; int result = 0; try { System.out.println("Attempting to parse value..."); result = Integer.parseInt(numbers[index]); System.out.println("Parsed value: " + result); } catch (NumberFormatException e) { System.out.println("Parsing failed: " + e.getMessage()); } finally { // This block always executes System.out.println("The 'finally' block has executed — cleanup or final steps go here."); } System.out.println("Program continues..."); } }

The finally block in Java is a special block that always executes after a try block, regardless of whether an exception was thrown or caught. It is used to ensure that important final actions, like closing resources or performing cleanup, are always performed.

In the example above, the program attempts to parse a value from an array. Even if parsing fails and the catch block executes, the finally block still runs, guaranteeing that the final message is printed and any necessary cleanup occurs. This makes the program more reliable and prevents resource leaks.

question mark

What is guaranteed about code placed inside a finally block in Java?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt