Professional Documents
Culture Documents
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);
}
}
Looks messy, doesn’t it? Now let’s try and beautify the code using the exception handlers – try
and catch:
try
System.out.println(3 / 0);
a: 0
a: 5
a: 2
a: 1
a: 1
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().
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.
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.
{
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");
}
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;
@Override
}
public class ExceptionHandling
catch (Exception e)
finally
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.
/*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();
}
class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
}
public class ExceptionHandling3
{
public static void main(String[] args)
{
try
{
ThrowsDemo.throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
//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
}
}