Exception Handling in JAVA
In this tutorial you will learn:
1) What is exception in JAVA?
2) What is throw block?
3) What is try and catch block?
What is exception in JAVA?
Exception is some unusual event that may happen in the program which also requires special attention of the programmer. Dealing with the exceptions require a lot of overhead processing. Not every error has to be indicated by an exception. Exception separates the code that deal with the exception from the code which is executed during normal running of the program. Exception lets us handle things more gracefully.
An exception in Java is an object that is created when an abnormal situation arises in your program. This object has members that stores information about the nature of the problem. An exception is always an object of some subclass of the standard class Throwable. Java provides a very well defined hierarchy of exceptions to deal with situations which are unusual. All standard exceptions are covered by two direct subclasses of the class Throwable. Class Error and its subclasses represent conditions that you are not expected to do anything about.
ThreadDeath, LinkageError and VirtualMachineError are the direct subclass of the class error. ThreadDeath error is thrown when an executing thread is deliberately stopped and when it is not caught the thread ends. Linkage error arises when we try to create an object of a class that does not exist. VirtualMachineError is thrown when failure of JVM occurs.
For all subclasses of Exception Class (except RuntimeException) we have to include code to deal with them. If we believe that our code may generate an error, we must handle it in our method or we may register then our code may throw an exception otherwise our code may not compile.
What is throw block?
If we have a method which can throw an exception that is neither a subclass of Error nor Runtime Exception, and we do not want to deal with it then you have to at least declare that this exception can be thrown. We can ignore the exception by enabling the method containing the exception throwing code to pass it on.
//Add a throws keyword after the parameter list of the method and list the classes for the //exceptions that might be thrown separated by comma
It any other method uses this method then it must deal with exception.
Throw , Try and Finally Block
If we need to handle the exception where they occur we use the try, catch and finally blocks. Try block encloses the code that may give rise to one or more exceptions. Code that can throw an exception that you want to catch must be in a try block, Catch block contains the code that is handling the exception of a particular type that may be thrown in a try block. Finally block contains code that is always executed before the method ends regardless of whether any exceptions are thrown in the try block.
//code that can throw exceptions
//code to handle the exception
// code to be executed last
Here it must be noted that the argument of the catch block must be of the type throwable or one of its subclasses. If the class you specify as the parameter has subclasses the catch block will be expected to process exceptions of that class plus all the subclasses of that class. When an exception is thrown the control is transferred immediately to the first statement in the appropriate catch block. After the catch block has executed, control goes to the statement after the catch block. You can catch an exception that may be any of two or
more different types in a single catch block.
We can also specify the possible types for the catch block parameter separated by |.
// Code that can throw exceptions of type ArithmeticException and IndexOutOfBoundException
// Code to handle exception of either type...
Even after catching an exception inside a catch block, we can pass on that exception to the calling program. We are rethrowing that exception from within the catch block, so we use throw statement
//The keyword throw is followed by the exception object to be thrown