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

Are arrays passed by reference in java?

492


Differentiate between run time error and syntax error.

573


What is meant by memory leak?

542


Can we assign null to double in java?

549


What does it mean to be immutable?

552






What is difference between path and classpath variables?

566


Write a program to calculate factorial in java?

583


What is mutable object and immutable object?

632


Why is it called a string?

567


How do you square a number in java?

558


What does provide mean construction?

552


What is the difference between an interface and an abstract class?

542


What is the use of beaninfo?

591


What is hotjava?

559


Why do we need hashmap in java?

559