0% found this document useful (0 votes)
64 views

Java Finally Block

The Java finally block is used to ensure that important cleanup code like closing connections or streams is always executed regardless of whether an exception is handled or not. Finally blocks follow try or catch blocks and are executed after any catch blocks. Finally blocks ensure resources are released even if an exception occurs. The document provides examples of finally block usage when exceptions do and do not occur and are handled or not handled.

Uploaded by

Hammad Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Java Finally Block

The Java finally block is used to ensure that important cleanup code like closing connections or streams is always executed regardless of whether an exception is handled or not. Finally blocks follow try or catch blocks and are executed after any catch blocks. Finally blocks ensure resources are released even if an exception occurs. The document provides examples of finally block usage when exceptions do and do not occur and are handled or not handled.

Uploaded by

Hammad Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1/31/2020 Java Finally block - javatpoint

Java finally block


Java finally block is a block that is used to execute important code such as
closing connection, stream etc.

Java finally block is always executed whether exception is handled or not.

Java finally block follows try or catch block.

Note: If you don't handle exception, before terminating the program,


JVM executes finally block(if any).

Why use java finally


Finally block in java can be used to put "cleanup" code such as closing
a file, closing connection etc.

Usage of Java finally


https://www.javatpoint.com/finally-block-in-exception-handling 1/3
1/31/2020 Java Finally block - javatpoint

Let's see the different cases where java finally block can be used.

Case 1

Let's see the java finally example where exception doesn't occur.

class
TestFinallyBlock{

Test it Now

Output:5
finally block is always executed
rest of the code...

Case 2

Let's see the java finally example where exception occurs and not
handled.

class
TestFinallyBlock1{

Test it Now

Output:finally block is always executed


Exception in thread main java.lang.ArithmeticException:/ by zero

Case 3

Let's see the java finally example where exception occurs and handled.

public class
TestFinallyBlock2{

Test it Now

Output:Exception in thread main java.lang.ArithmeticException:/ by zero


finally block is always executed
rest of the code...

Rule: For each try block there can be zero or more catch blocks, but
only one finally block.
https://www.javatpoint.com/finally-block-in-exception-handling 2/3
1/31/2020 Java Finally block - javatpoint

Note: The finally block will not be executed if program exits(either by


calling System.exit() or by causing a fatal error that causes the
process to abort).

← prev next →

Help Others, Please Share

https://www.javatpoint.com/finally-block-in-exception-handling 3/3

You might also like