You are on page 1of 13

INDEX

EXCEPTION HANDLING Page 2

USING TRY-CATCH Page 3

EXCEPTION CLASS HIERARCHY Page 5

EXCEPTION CATEGORIES Page 5

MULTIPLE CATCH CLAUSES IN THE SAME CODE Page 6

THE FINALLY CLAUSE Page 7

TRY WITH RESOURCES Page 8

SUPPRESSED EXCEPTIONS Page 9

DECLARING EXCEPTIONS Page 12

HANDLING DECLARED EXCEPTIONS Page 12

THROWING EXCEPTIONS Page 13


1. EXCEPTION HANDLING

A java exception is an object that describes an exceptional condition (error) that has
occurred in a piece of code. A non-syntactical error will not be caught by the compiler.
(Except for checked exceptions which we will come across later) For example, when we
were discussing virtual method invocation (Polymorphism), in case of some downward
casts, although the code compiled perfectly, but still a ClassCastException was thrown at
the time of execution. That is why reliable applications should handle exceptions as
gracefully as possible. Exceptions can occur not only as a result of application bugs like
accessing an array index that is not defined or out of bounds, but also because of some
factors beyond the control of the application. These include databases becoming
unreachable or failure of the hard drives.

Handling an exception means that we are inserting a separate code block to deal with
the exception/error which would tell the interpreter what steps to follow in case it
encounters that exception.

Declaring an exception simply means that we are indicating that a method/function may
fail to execute successfully.

Java exception handling is managed by 5 keywords namely try, catch, throw, throws and
finally. Program statements that we want to monitor are written within the try clause. If
an exception is generated by the code block which we are monitoring, then that
exception can be caught by the catch clause which will tell the Java Runtime
Environment what subsequent actions are to be taken. So, by handling the exceptions
we are not implicitly allowing the Java Runtime Environment to throw a system
generated exception. Rather we are explicitly providing code to the application so that if
a client is working with an application and some method throws an exception, it tells
him exactly what the problem is. To manually throw an exception, we make use of the
keyword throw. Any exception that is manually thrown must be specified as such by the
throws clause. Any code that must absolutely be executed should be put inside the
finally clause.
2. USING TRY-CATCH
In the following program, I am simply trying to divide 3 by 0.
package exceptionhandling;
public class ExceptionHandling
{
public static void main(String[] args)
{
System.out.println(3 / 0);
}
}

OUTPUT: Exception in thread "main" java.lang.ArithmeticException: / by zero

Looks messy, doesn’t it? Now let’s try and beautify the code using the exception handlers – try
and catch:

public class ExceptionHandling

public static void main(String[] args)

try

System.out.println(3 / 0);

catch(ArithmeticException e) //ArithmeticException is an Exception class

System.out.println("Division by zero is undefined!");

OUTPUT: Division by zero is undefined!


Consider another program.

public static void main(String[] args)


{
int a = 0;
for (int i = 0; i < 10; i++)
{
try
{
a = 5 / i;
}
catch (ArithmeticException e)
{
System.out.println(e); //exception string generated by JRE.
a = 0; // set a to zero and continue
}
System.out.println("a: " + a);
}
}

OUTPUT: java.lang.ArithmeticException: / by zero

a: 0

a: 5

a: 2

a: 1

a: 1

So, the catch clause should be used to:

 Retry the operation.


 Try an alternate operation.
 Gracefully exit or return.

Note that an object of Exception class has been passed as an argument to the catch clause. The
Exception class also provides several pre-defined methods such as getMessage() and
printStackTrace().

3. EXCEPTION CLASS HIERARCHY


Extends Object

Exception Error
Child Dependent Unchecked

4. EXCEPTION CATEGORIES
OutOfMemory
RuntimeException IOException Thread Death
 Error
Checked exceptions, which must be handled or declared.
Unchecked exceptions, which need Checked Unchecked Unchecked
Unchecked
 not be handled or declared.

java.lang.RuntimeException and java.lang.Error and their subclasses fall in the category of


ArrayIndex OutOf
unchecked exceptions.
ClassCastException We should avoid catching the base type of exception because it is
BoundsException
difficult to create a general
Unchecked purpose catch block that can deal with every possible error. We
Unchecked
could have taken an Exception type parameter instead of an ArithmeticException type but that
should be avoided.
NullPointerException ArithmeticException

Unchecked Unchecked
5. MULTIPLE CATCH CLAUSES IN THE SAME CODE
public class ExceptionHandling
{
public static void main(String[] args)
{
try
{
System.out.println("about to open a file");
InputStream in=new FileInputStream("missingfile.txt");
int data=in.read();
}
catch(FileNotFoundException e)
//catch containing child class object declared before.
{
System.out.println(e.getClass().getName());
System.out.println("Quitting");
}
catch(IOException e)
//catch containing parent class object declared after.
{
System.out.println(e.getClass().getName());
System.out.println("Quitting");
}
}
}

In the above code we are handling exceptions related with I/O streams which generate checked
exceptions. This means that unlike the Arithmetic Exception try-catch code block, the IO
Exception try-catch code block is essential without which the program will not execute. In the
above code, we have created an object of InputStream and are trying to read the data present
in a text file using the read() method. But since no such file by the name of missingfile.txt exists,
the statement which is trying to create the object will generate an exception which the
compiler check. So, if we don’t handle the exception, then a compile-time error will be
generated. Also note the order of exceptions in the catch clause. The catch clause containing
the FileNotFoundException (subclass of IOException) should always be declared first. Although
the it was not necessary to declare that the main() method throws an exception (using the
throws keyword), in the next program it becomes necessary.
6. THE FINALLY CLAUSE
When we open resources such as files or database connections, we should always close
them when they are no longer needed. Attempting to close these resources inside the
try block can be very problematic because if an exception is encountered before the
close keyword, then it will be skipped as the control jumps to the subsequent catch
block. Here we make use of the finally clause. A finally block always executes regardless
of whether or not an exception was encountered during the execution of the try block.

public class ExceptionHandling

{
static InputStream in;
public static void main(String[] args) throws IOException
{
try
{
in = new FileInputStream("missingfile.txt");
System.out.println("about to open a file");
}
catch (IOException e )
{
System.out.println(e.getClass().getName());
System.out.println("Quitting");
}

finally //for closing the file

try //necessary because in points to a null object


{
in.close();
}
catch(NullPointerException e)
{
System.out.println(e.getClass().getName());
}
}
}
}
7. TRY WITH RESOURCES
JAVA SE 7 provides an advanced type of try known as the try with resources statement
which auto closes the resources (files or database connections) thereby eliminating the
need for writing a lengthy finally code block. So the following code is perfectly valid and
will not cause any problem:

public class ExceptionHandling


{
public static void main(String[] args)
{
try(InputStream in=new FileInputStream("missingfile.txt"))
{
System.out.println("about to open a file");
int data=in.read();
}
catch(IOException e)
{
System.out.println(e.getClass().getName());
System.out.println("Quitting");
}
}
}

Note the omission of the close() method here. Resources in a try-with-resources statement
must implement either java.lang.AutoCloseable interface (new in JDK 7) or java.io.Closeable
interface (refactored in JDK 7 to extend AutoCloseable).

 If an exception occurs while creating an auto closeable resource, then the control will
immediately jump to the catch block.
 If an exception occurs in the body of a try block, all resources will be closed before the
catch block runs.
 If the try block executes without any exceptions but an exception is generated during
the closing of the resource, then the control will jump to the catch block.
 If both the try block and the closing of resources generate exceptions, then the latter
exception will be suppressed. (Discussed in the next topic)
8. SUPPRESSED EXCEPTIONS

If an exception occurs while creating the AutoCloseable resource, control will immediately
jump to a catch block. If an exception occurs in the body of the try block, all resources will
be closed before the catch block runs. If an exception is generated while closing the
resources, it will get suppressed. I will reiterate the program which I mentioned in my
previous document:

package exceptionhandling2;

class SwingException extends Exception {}

class OpenDoor implements AutoCloseable //for overriding the close() method.

public OpenDoor() throws Exception //constructor

System.out.println("The door is open.");

public void swing() throws Exception

System.out.println("The door is becoming unhinged.");

throw new SwingException(); //this will be caught by catch clause.

@Override

public void close() throws Exception //method of AutoCloseable

System.out.println("The door is closed.");

}
public class ExceptionHandling

public static void main(String[] args)

try (OpenDoor door = new OpenDoor()) //Creation of resources.

door.swing(); //this throws a SwingException

catch (Exception e)

System.out.println("Is there a draft? " + e.getClass());

finally

System.out.println("I'm putting a sweater on, regardless. ");

OUTPUT: The door is open.

The door is becoming unhinged.

The door is closed.

Is there a draft? class exceptionhandling2.SwingException

I'm putting a sweater on, regardless.


You should observe the order of execution of the statements.

 An OpenDoor resource/object is created within the try clause. It’s constructor is


invoked.
 The swing() method is invoked which throws an exception.
 Before the execution of the catch block, the close() method is invoked which autocloses
the open resources.
 The control now shifts to the catch clause where the exception thrown by the swing()
method is caught and then the required steps are performed.
 The finally clause is executed regardless of the fact whether any exception was
generated or not.

QUES) What will happen if 2 exceptions are generated by the code i.e one by the try block as
shown above and another by the close() method when the resources are being autoclosed?
ANS) We will get the same output as above because the Exception generated upon the closure
of resources will get suppressed automatically. So, only the exception generated by the try
block will be caught be the catch block. To display the exception thrown by the close() method,
we can add the following code lines inside the catch clause:

for(Throwable t:e.getSuppressed())

System.out.println(t.getMessage());

JAVA SE 7 also provides a new multi-catch clause in which we can use the same catch clause for
catching different kinds of exceptions. The syntax is:

……….

catch(ClassNotFoundException|IOException)

//code

………. Note: the type alternatives separated by vertical bars cannot have an inheritance
relationship.
9. DECLARING EXCEPTIONS
If we don’t wish to handle a checked exception, we should at least declare it by
indicating to the compiler that the method will throw an exception. This is done using
the throws keyword.

public static void main(String[] args) throws IOException


{
try(InputStream in=new FileInputStream("missingfile.txt"))
{
System.out.println("about to open a file");
//InputStream in=new FileInputStream("missingfile.txt");

/*If we uncomment the above line and get rid of the try with resources utility, then the
program will not compile because a try cannot actually work without a catch clause. The try
with resources is simply used here to close the open resources.*/

int data=in.read();
}

10. HANDLING DECLARED EXCEPTIONS


Even if we have declared that a method will throw an exception, it is again our very own
responsibility to handle it.

class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");

/*Here, new is used to construct an instance of IllegalAccessException. All of Java’s


built-in run-time exceptions have at least two constructors: one with no parameter
and one that takes a string parameter.*/

}
}
public class ExceptionHandling3
{
public static void main(String[] args)
{
try
{
ThrowsDemo.throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}

11. THROWING EXCEPTIONS


We can re-throw an exception that has already been caught. Let us modify the above
code a bit:

public static void main(String[] args) throws IllegalAccessException

//It is necessary to declare that the main() method will throw an exception

{
try
{
ThrowsDemo.throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
throw e; //Exception re-thrown
}
}

You might also like