Contents

Exception Handling

The goal of exception handling is to write large and reliable programs so that your
programs should not have any error which is remains unhandled. By providing an
effective error-handler model using exception handling, Java allows the program
code to convey the problems to the client more effectively.
Without exception handling model, you need to write code check a particular
error and manage it multiple times at multiple places in your program. Using
exception handling, code can be reduced and it also separates the code that
performs a specified task from the code that will be executed when the exception
is thrown.
One of the interesting aspect of exception handling is that if some run time
error occurs, it does not allow the programs to continue on its ordinary path.
In fact, it changes the path of execution and reports the user about the error
occurred and force the program to handle the error and continue the execution
process.
When an exception is thrown, the control is redirected to some place of code
to handle the problem, that place is known as exception handler.When an exception is thrown, an Exception object is created on the heap
using keyword new. Thus, it can be said that
“exceptions are objects”in Java.

Handling Exceptions

To prevent a program from terminating abnormally, you must write the doubtful
code inside the guarded region. The guarded region can be created using try block,
as shown below.
The guarded region (try block) must be followed by catch block that handles the
exception, known as exception handler, as shown below:

Instruction
Now interpret this program without any command line argument. It
generate the following output:

Output:
Start
Division by zero is not valid.
Completed

Java.lang.Throwable Class

All Exception classes are derived from java.lang Throwable class.
On next
page, you can see the partial hierarchy of classes derived from Throwable class.
Throwable class provides a String variable i.e.
detailMessage.This variable
consists of specific details about the runtime error. For example, for FileNotFound
Exception, it consist of the file name that could not be found, and for
ArithmeticException it contains “/ by zero” message.
Throwable class provides the following commonly used methods:

public String getMessage()

This method returns the string message stored in detailMessage variable.

public String toString()

It returns name of the exception class along with the error message from
detailMessage variable.

public void printStackTrace()

As you have seen in program (StackTrace.java), it shows the information
about invoked methods from the runtime stack.

public StackTraceElement[] getStackTrace()

It returns an array of stack trace elements, each represents one stack frame.

Java.lang.Exception Class

Java.lang.Exception is a subclass of java.lang.Throwable.
Class RuntimeException,
IOException, SQLException and
PrinterException etc are sub classes
of Exception class.
It provides the following commonly used constructors:

public Exception (String msg)

public Exception (Throwable obj)

public Exception (String msg, Throwable obj)

When any of the constructors is invoked, it sends the message to the corresponding
constructor of java.lang.Throwable class. Actually, the constructors of
Throwable class are responsible for creation of
Exception object and also sets
the string message to detailMessage variable of Throwable

Using finally Block

If an exception is thrown inside a try block and there is no matching catch block
then the program terminates without executing remaining lines of code. But some
times, it is desirable to execute some code in the following three situations:

Using Multiple catch Blocks

A try block can throw different types of exceptions. It depends upon the tasks
you perform inside try block. For example, accessing array elements and doing
arithmetic operations etc.
If you are using only one catch block, it can handle exception of a specific type.
The type depends upon the Exception class specified in parenthesis of catch
block.
So to handle multiple exceptions, you should use multiple catch blocks with
different Exception Classes in parenthesis, as shown below:

17 Years experience in Training & Development.
Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android. If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email :[email protected]#Contact : 98722-46056