Skip to main content

Finally Block

The finally block in Java is used to execute code that should always run, regardless of whether an exception occurs or not. It is commonly used for cleanup activities, such as closing files, releasing resources, or ensuring that certain actions are performed even if an error occurs.

In this section, we’ll explore:

  • The syntax and purpose of the finally block.
  • Scenarios where the finally block does not execute.
  • Practical examples of using the finally block.

Syntax of the Finally Block​

The finally block is placed after the try-catch block. It ensures that specific code always executes, whether an exception is thrown or not.

Basic Syntax​

try {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that always executes
}

Example: Using Finally for Cleanup​

public class FinallyExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This will always execute.");
}
}
}

Output:

Error: / by zero
This will always execute.

Explanation:

  • The try block throws an ArithmeticException.
  • The catch block handles the exception.
  • The finally block executes regardless of whether an exception occurred.

Purpose of the Finally Block​

The finally block is primarily used for:

  1. Cleanup Activities:
    • Closing files, database connections, or network sockets.
  2. Ensuring Execution:
    • Guaranteeing that critical code runs even if an exception occurs.

Example: Closing a File in Finally​

import java.io.FileWriter;
import java.io.IOException;

public class FileCloseExample {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("output.txt");
writer.write("Hello, World!");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
try {
if (writer != null) {
writer.close(); // Ensure the file is closed
System.out.println("File closed successfully.");
}
} catch (IOException e) {
System.out.println("Error closing file: " + e.getMessage());
}
}
}
}

Output (if no errors occur):

File closed successfully.

Explanation:

  • The finally block ensures that the FileWriter is closed, even if an exception occurs during file writing.

Scenarios Where Finally Does Not Execute​

While the finally block almost always executes, there are rare scenarios where it does not:

  1. System Exit:

    • If System.exit() is called in the try or catch block, the program terminates immediately, and the finally block does not execute.
    public class SystemExitExample {
    public static void main(String[] args) {
    try {
    System.out.println("Inside try block.");
    System.exit(0); // Terminates the program
    } catch (Exception e) {
    System.out.println("Inside catch block.");
    } finally {
    System.out.println("Inside finally block."); // This will NOT execute
    }
    }
    }

    Output:

    Inside try block.
  2. Infinite Loop or Crash:

    • If the program enters an infinite loop or crashes due to a critical error (e.g., OutOfMemoryError), the finally block may not execute.

Best Practices for Using Finally​

  1. Use Finally for Cleanup:

    • Always use the finally block for resource cleanup, such as closing files or database connections.
  2. Avoid Complex Logic in Finally:

    • Keep the finally block simple and focused on cleanup activities. Avoid adding complex logic that could introduce new exceptions.
  3. Consider Try-With-Resources (Java 7+):

    • For managing resources like files or streams, consider using the try-with-resources statement, which automatically closes resources without requiring a finally block.
    import java.io.FileWriter;
    import java.io.IOException;

    public class TryWithResourcesExample {
    public static void main(String[] args) {
    try (FileWriter writer = new FileWriter("output.txt")) {
    writer.write("Hello, World!");
    } catch (IOException e) {
    System.out.println("Error: " + e.getMessage());
    }
    // No need for finally block; resources are automatically closed
    }
    }

Key Takeaways​

  • The finally block ensures that specific code always executes, regardless of whether an exception occurs.
  • It is commonly used for cleanup activities, such as closing files or releasing resources.
  • The finally block does not execute in rare scenarios, such as when System.exit() is called or the program crashes.
  • Best practices include using finally for cleanup, avoiding complex logic, and considering try-with-resources for resource management.