CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > finally Block in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

finally Block in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X
Finally Block in Java

The finally block in Java is a block of code that is always executed, regardless of whether an exception is thrown or not. This makes it a useful place to put code that needs to be executed, even if an error occurs.

For example, the finally block is often used to close resources, such as files or database connections. This ensures that the resources are properly released, even if an exception is thrown during the execution of the try block.

The finally block is also sometimes used to perform cleanup tasks, such as resetting variables or updating status flags.

Let’s see an example of finally block without exception:

public class FinallyBlockExample {

  public static void main(String[] args) {
    try {
      System.out.println("Inside the try block");
      int data = 5 / 1;
    } catch (ArithmeticException e) {
      System.out.println("Exception caught: " + e.getMessage());
    } finally {
      System.out.println("This code is always executed");
    }
  }
}

Output:

Inside the try block
This code is always executed

Now, let’s see the output of the following program with the exception:

public class FinallyBlockExample {

  public static void main(String[] args) {
    try {
      // Code that may throw an exception
      System.out.println("Inside the try block");
      int data = 5/ 0;
    } catch (ArithmeticException e) {
      System.out.println("Exception caught: " + e.getMessage());
    } finally {
      System.out.println("This code is always executed");
    }
  }
}

The output of the code is:

Inside the try block
Exception caught: / by zero
This code is always executed

As we can see from both the examples, the finally block is executed even though an exception was thrown in the try block.

FAQs

What are some common use cases for the finally block?

Common use cases for the finally block include closing files, releasing resources (e.g., database connections), cleanup tasks, and ensuring that critical code always executes, even if exceptions occur.

Can a finally block exist without a corresponding try block?

No, a finally block must be associated with a try block. It is used to specify what code should execute after the try block (and optional catch blocks) completes its execution.

What happens if an exception is thrown in the finally block itself?

If an exception is thrown within the finally block, it will override any exception that may have been thrown earlier in the try or catch blocks. The new exception will be propagated, and the original exception may be lost.

Can I have multiple finally blocks following a single try block?

No, you can have only one finally block following a try block. However, you can have multiple catch blocks to handle different types of exceptions that may be thrown in the try block.

Is it mandatory to include a finally block in a try-catch structure?

No, it’s not mandatory to include a finally block in a try-catch structure. You can have a try block with or without a catch block, and you can also have a try-finally structure without any catch blocks.

Related Posts:

  • Control Statements in Java
  • What is Try-With-Resources in Java? A Complete Guide
  • Exception Handling Keywords in Java
  • Top 10 Common Java Errors
  • MySQL Commands for Developers
  • Exception Handling in Java
Tags:exceptionjavalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleThrowing and re-throwing an Exception in Java
Next Article โ†’How To Print Git Commit Message In Jenkins Pipeline

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap