What is 'finally' method in Exceptions?

Answer Posted / sumesh babu r

The finally method will be executed after a try or catch
execution.
It is mainly used to free up resources.
The codes that must be executed, irrespective of whether the
exception is occurred or not, will be included in the
finally block.

See the following simple example to demonstrate the syntax
public class FinallyExample
{

public static void main(String args[])
{

try
{
// the code that may cause an exception
}
catch (Exception e)
{
// the code to be executed, when the exception
occurs
}
finally
{
// code to be executed irrespective of the
occurrence of exception
}
}
}

When using finally, the catch block is not mandatory.
ie, a try block must be followed by a catch or finally block.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why pass by reference is not possible in java?

710


Can we compare two strings in java?

764


what is deadlock? : Java thread

733


Is it possible to instantiate the abstract class?

729


What is the program compilation process?

777


What is stack example?

773


How do you use equal in java?

732


How do you implement singleton class?

740


Can we call a non-static method from inside a static method?

777


Can we force garbage collector to run ?

775


Explain the difference between throw and throws in java?

775


What is generic type?

789


what is synchronization and why is it important? : Java thread

841


How do you declare an array in java?

744


Can we execute a program without main?

747