Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende The finally Block | Handling Exceptions in Java
Quizzes & Challenges
Quizzes
Challenges
/
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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 10

bookThe finally Block

Desliza para mostrar el menú

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
some-alt