Friday, May 13, 2016

Java Try Catch Finally


This is a sample code to show the use of try | catch and finally block. We can execute some unsure code / dangerous code inside a try block. If that code fails / breaks / throws exception then catch block would be executed, any code within the catch block would be executed, and if no exception is thrown by the code within the try block catch block is not executed. However irrespective of where the code within try is trowing exception or even not throwing exception the finally block is always executed.

When I say a<b (condition is true) 
try block executed
if block executed
catch block executed
finally bloc executed


When I say a>b (condition false)
try block executed
finally bloc executed


if you want the finally block not to execute when there is no exception for the code in try block then use System.exit(new Integer(0)); at try block


No comments:

Post a Comment