---
title: "Does a finally block always get executed in Java?"  
description: "Does a finally block always get executed in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159222/does-a-finally-block-always-get-executed-in-java  
category: "java"  
tags: ["java", "code"]  
reading_time: 2 minutes  

---

# Does a finally block always get executed in Java?

Does a [finally block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block) always get executed in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

Yes, a finally [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system) always gets executed in Java, regardless of whether an exception is thrown or not.

The finally block is a block of code that is executed after the try and catch blocks. It is used to perform clean-up tasks, such as closing files or releasing resources.

The following code shows how to use a finally block:

```plaintext
public class Main {

  public static void main(String[] args) {
    try {
      // Do something that might throw an exception
    } catch (Exception e) {
      // Handle the exception
    } finally {
      // Perform clean-up tasks
    }
  }
}
```

In the code above, the finally block will always be executed, regardless of whether or not an exception is thrown in the try block.

Here are some important things to note about finally blocks:

- The finally block is always executed, even if an exception is thrown in the try block.
- The finally block is executed after the catch block, if an exception is thrown.
- The finally block is executed even if the try block is terminated using the `break` or `return` statements.

Finally blocks are a powerful tool that can be used to ensure that clean-up tasks are always performed, regardless of whether or not an exception is thrown.


---

Original Source: https://www.mindstick.com/forum/159222/does-a-finally-block-always-get-executed-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
