try-catch-finally block in java programming
try block
To handle a run time error and monitor the results,we simply enclose the code inside a try block.If an exception occurs within the try block,it is handled by the appropriate exception handler (catch block) associated with the try block.A try block should have one (or more) catch blocks or one finally block or both.Exception handlers are put in association with a try block by providing one or more catch block directly after the try block
Syntax of try catch and finally block
try {
//java statements(risky code)
}
catch(Exception e)
{
//handled code
}
finally
{
//cleanup code
}
catch block
finally block
The code within the finally block is always executed regardless of what happens within the try block .That is , if no exceptions are thrown, then no part of the code in the catch blocks is executed but code in the finally block is executed.
Super
ReplyDelete