answersLogoWhite

0

The Finally block in java is used along with the try-catch statements.

The usual structure is

try {

.....

} catch(Exception e){

....

} finally {

.....

}

In the finally block we usually have code that is used to perform clean up activities corresponding to the code in the try block. When an exception occurs in the try block control comes to the catch block and the rest of the code in the try block would not execute. In such cases we may need to have some code that cleans up the objects that we created/used in the try block.

No matter what happens, the code inside the finally block would definitely execute.

The best use of finally block is when we are connecting to the database. In the try block we would have the statements that instantiate the connection, prepared statement, result set etc. If the query fails then all these objects would be left open and the code would continue. So in the finally block we usually nullify these statements so that even in case of an error the unused objects are cleared.

Example:

Connection con;

PreparedStatment PS;

ResultSet rs;

try{

con = ConnectionManager.getConnection();

PS = con.prepareStatement(query);

rs = PS.executyQuery();

......

} catch (Exception e){

e.printStackTrace();

} finally {

con.close();

PS.close();

}

In case of an error the connection & prepared statement objects would remain open if we do not have the finally block.

Tip: Whatever code we have inside the finally block would be run definitely if the try block is invoked. We can even use the finally block to check if the try block was reached and executed. If the try executes then we can rest assured that the finally would also get executed.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is the difference using finally block and without finally block?

use of finally block is optional in java.if u want to clean up ur code (means u want to close any connection)after exception handling then go for finally block.if not then also your code will run fine.so finally block is optional in java.


What is the return type of finally method in java?

The final and finally keywords have no impact on the return type of a method in Java.


What is the first name before of the Java Program?

The first name of the Java Programming was Oak. It then went by the game Green and finally Java from Java coffee.


What is the first name before of the Java Programming?

The first name of the Java Programming was Oak. It then went by the game Green and finally Java from Java coffee.


What are block of statements in java?

In computer programming, a block or code block is a lexical structure of source code which is grouped together. In Java it is sed in the same way: to group code and separate it from the rest.


Which is equivalent to try and finally block?

Try block is followed by finally block as an alternative . But usually it is followed by a catch block. Both are equivalent.


Why do you use synchronized block in java?

A Synchronized block is a block of code enclosed by the {} braces that has a synchronized keyword before it. This block of code is similar to a synchronized method and only one thread can access it at a time.


The finally block always executes when the try block exitswhy?

Why does this happen? Because the people who designed Java said this should be so. They wanted a place to put cleanup code which was (almost) guaranteed to run no matter what exceptions were thrown along the way.


What is mean by component in java?

A component is a Reusable building block .


Can you escape finally block?

You can only explicitly escape from a finally block by returning out of it. A break statement is not allowed there.


What is the purpose of catch block in java?

to provide an action in case the code block in the try statement fails.


What is the use of catch block in java?

the catch block catches the exception from the try block to display a proper message about the exception. Answered by, SOORAJ.M.S