Subscribe
Tutorial search

The finally clause with try and catch - Java tutorial


This tutorial shows you how to try and catch the exceptions and use the finally clause
Category: Java tutorials - Difficulty:




Using the finally Clause

When an exception is raised, the statements in the try block written after the statement in which the exception occurred are ignored. The finally block is used to process certain statements, no matter whether an exception is raised or not. The block of code attached with the finally clause is executed after a try-catch block has been executed.

try

{

//block of code

}

finally

{

// block of code that is always executed irrespective of an exception being raised or not

}

If there is a catch block associated with the try block, the finally clause is written after the catch block.

try

{

//block of code

}

catch (Exception obj)

{

//block of code

}

finally

{

//block of code that is always executed irrespective of an exception being raised or not

}