Java finally

Amansingh Javatpoint
1 min readJun 11, 2021

--

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection must be put inside the Java finally block. The Java finally block ensures that whatever statements are present in the finally block must be executed irrespective of the exception raised in the program or not. The Java finally can be used with the try-catch or can be used only with the try keyword.

Java finally syntax

The syntax of the finallykeyword is mentioned below.

try

{

// Statements that are potent to cause an exception

}

catch

{

// for Handling the raised exception

}

finally

{

// Statements that have to be executed irrespective of an exception raised or not

}

We can also use finally without using catch.

try

{

// Statements that are potent to cause an exception

}

finally

{

// Statements that have to be executed irrespective of an exception raised or not

}

--

--

No responses yet