JAVA- Handling the exceptions

To handle an exception condition the statement that may contain exceptional conditions is written in the ‘try’ block. This block is followed by one or more ‘catch’ blocks and optional ‘finally block’. ‘try’ block contains the statements that may result in abnormal conditions i.e. statement which can throw an exception.

The exception i.e. thrown within the ‘try’ block is caught by the matching catch block and statements in the corresponding ‘catch’ block are executed. The ‘finally’ if specified is always executed whether an exception occurs or not.

The general format of exception handling construct is as follows:

try

{

//statements that may cause exception.

}

catch(exception class  exception object)

{

//exception handler program body

}

catch(exception class2  exception object2)

{

//exception handler program body

}

finally

{

//must execute program body

}       

 

 Finally: The ‘finally’ if specified is always executed whether an exception occurs or not.

Leave a Reply

Your email address will not be published. Required fields are marked *