How do we Handle Exception in Java

Exception Handling in Java

In the previous tutorial, we have discussed about what is an Exception and different types of exceptions in Java. In this tutorial we are going to see how to handle the Exception in Java.

Typical Exception Handling Work Flow :

To handle exceptions, Java gave us following keywords.

try

catch

finally

throw

throws

try block

In Java when we are not sure about the outcome of a statement, we can try out that statements (not sure about the out come) by placing in a try block. A try block can be used in a method or an another try block.

catch block

In Java catch block is used to handle the exception. While executing the statements in try block, if the system encounters an exception then it skips out rest of the statements and transfer the control to corresponding catch block. Hence the catch block is executed, if and only if an exception is raised other wise it will not be executed.

Once the control comes out from the try block, there is no way of taking it back in to try block.

finally block

A finally block is a block of statements, that are executed irrespective of whether we get an exception or not. Hence the statements in the finally block are executed compulsory. Typically finally blocks are used to nullify the object references and closing the io streams.

Hi
Folks,
you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,