---
title: "What is the purpose of the finally block in Java exception handling?"  
description: "What is the purpose of the finally block in Java exception handling?"  
author: "Steilla Mitchel"  
published: 2023-07-23  
updated: 2023-07-24  
canonical: https://www.mindstick.com/forum/159235/what-is-the-purpose-of-the-finally-block-in-java-exception-handling  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 2 minutes  

---

# What is the purpose of the finally block in Java exception handling?

What is the [purpose](https://yourviews.mindstick.com/view/247/no-fail-policy-failing-its-purpose) of the [finally block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling)?

## Replies

### Reply by Aryan Kumar

The finally [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system) in Java [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) [handling](https://www.mindstick.com/forum/34585/file-handling) is used to execute code that must be executed regardless of whether or not an exception is thrown. The finally block is always executed, even if an exception is thrown and not caught.

The finally block is often used to close resources that were opened in the try block. For example, if you open a file in the try block, you should close it in the finally block to prevent resource leaks.

Here is an example of how to use the finally block in Java:

Java

```plaintext
public class FinallyBlockExample {

  public static void main(String[] args) {
    try {
      // Code that may throw an exception
    } catch (Exception e) {
      // Handle the exception
    } finally {
      // Close any resources that were opened
    }
  }
}
```

In this example, the main() method contains a try block that contains code that may throw an exception. If an exception is thrown, the catch block will be executed. The catch block will handle the exception, and then the finally block will be executed.

The finally block is executed regardless of whether or not an exception is thrown. In this example, the finally block will close any resources that were opened in the try block.

Here are some other things to keep in mind about the finally block:

- The finally block is always executed, even if an exception is thrown and not caught.
- The finally block is executed after the try block and any catch blocks.
- The finally block can be used to close resources, perform cleanup, or perform any other task that must be executed regardless of whether or not an exception is thrown.

By following these guidelines, you can ensure that your code is properly handling exceptions and that resources are closed properly.


---

Original Source: https://www.mindstick.com/forum/159235/what-is-the-purpose-of-the-finally-block-in-java-exception-handling

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
