Chapter 5 Exception Handling
Chapter 5 Exception Handling
Handling
Writing Robust Java Codes
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 1
College
Introduction
• Two cases exist when you design & code a
method
The case where nothing unusual happens and
The case where exceptional things happen.
• In Java, there is a way to deal with cases
where exceptional things can happen.
• The way is known as exception handling.
• It deals with methods that have some special
case that is handled differently depending on
how the method is used.
Example: a method that handles division by zero.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 2
College
Exception Types
• Exceptions are objects.
• Any class designed for throwable objects
must extend the class Throwable or one of its
subclasses.
• The Throwable class contains a string that
can be used to describe the exception it.
• Exceptions can be of two types:
Checked Exceptions: the compiler checks that
your methods throw only the exceptions they
have declared themselves to throw.
Unchecked Exceptions: conditions that reflect
errors
CS_Dept@Sata in your program's logic and cannot be
Technology
Object Oriented Programming - CoSc 2082 3
College
reasonably recovered from at run time.
Exception types…
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 4
College
Checked Exceptions
• Represent conditions that, although
exceptional, can reasonably be expected to
occur.
• If they do occur must be dealt with in some
way.
• Checking them ensures that the caller of a
method deals with the exception in some
way or at least consciously chooses to ignore
it.
• We have to explicitly specify these exception
during method definition.
Eg. RemoteException
CS_Dept@Sata Technology
College in RMI- CoSc 2082
Object Oriented Programming 5
Unchecked Exceptions
• Reflect errors in your program's logic.
• Cannot be reasonably recovered from at run
time.
• These are errors that should be corrected in
the program code.
• These are errors that should be corrected in
the program code.
• They are also called Runtime Exceptions.
Eg. ArrayIndexOutOfBounsException: thrown
when you access outside the bounds of an array
• You might define a new class of runtime
CS_Dept@Sata Technology
exception that Object
College is specific to your application 6
Oriented Programming - CoSc 2082
Error Class
• Defines a range of errors that indicate
something has failed in the virtual machine
itself (VirtualMachineError), or
• In the virtual machine's attempt to execute
your application (LinkageError).
• These are also unchecked because they are
beyond the applications ability to control, or
handle.
• Application code should rarely, if ever, throw
these error exceptions directly.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 7
College
How to handle
exceptions
• In Java, exception handling proceeds as
follows:
• Either some library software or your code
provides a mechanism that signals when
something unusual happens. This is called
throwing an exception.
• At another place in your program you place
the code that deals with the exceptional case.
This is called handling the exception.
• This method of programming makes for
cleaner code.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 8
College
try-throw-catch
mechanism
• The basic way of handling exceptions in Java
consists of the try-throw-catch trio.
• The general setup consists of a try block
followed by one or more catch blocks.
• A try block has the syntax
try{
Some_Code
}
• This try block contains the code for the basic
algorithm that tells what to do when
everything goes smoothly.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 9
College
try block….
• It “tries” to execute the case where all goes
smoothly.
• If something exceptional does happen, you
want to throw an exception.
Indicates that something unusual happened.
• The basic outline, when we add a throw, is as
follows:
try{
Code_That_May_Throw_An_Exception
}
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 10
College
throw...
• Example:
try{
if(denominator==0)
throw new Exception(“Division by zero”);
}
• The value thrown is an argument to the
throw operator and is always an object of
some exception class.
• The execution of a throw statement is called
throwing an exception.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 11
College
catch block
• A catch block begins execution when an
exception is thrown in the try block.
• The catch block has a parameter.
• Exception object thrown is plugged in for this
catch block parameter.
• Executing of the catch block is called catching
the exception or handling the exception.
• When an exception is thrown, it should ultimately
be handled by (caught by) some catch block.
• Catch block immediately follows the try block.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 12
College
catch block…
• Syntax of catch block looks like as follows:
catch (ExceptionClassName e){
//code to be executed in the catch block
}
• Example:
catch(Exception e){
String message = e.getMessage();
System.out.println(message);
System.exit(0);
}
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 13
College
Declaring Exceptions in a throws
Clause
• If a method does not catch an exception, it
must at least warn that any invocation of the
method might possibly throw an exception.
• This warning is called a throws clause.
• Including an exception class in a throws
clause is called declaring the exception.
For example, a method that might possibly throw
a DivisionByZeroException and that does not
catch the exception would have a heading similar
to the following:
• public void sampleMethod() throws
DivisionByZeroException
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 14
College
Declaring exceptions…
• If there is more than one possible exception that
can be thrown in the method definition, then the
exception types are separated by commas.
• public void sampleMethod() throws DivisionByZeroException,
SomeOtherException
• This technique is a form of shifting responsibility
(“passing the buck”).
• If you define a method that might throw
exceptions of some particular class, then normally
either
Your method definition must include a catch block that
will catch the exception or
You must declare (that is, list) the exception class within
a throws
CS_Dept@Sata Technologyclause.
Object Oriented Programming - CoSc 2082 15
College
Catch or Declare Rule
• Most “ordinary” exceptions that might be
thrown when a method is invoked must be
accounted for in one of two ways:
The possible exception can be caught in a catch
block within the method definition.
The possible exception can be declared at the
start of the method definition by placing the
exception class name in a throws clause.
• This is known as the Catch or Declare Rule.
• In any one method, you can mix the two,
catching some exceptions and declaring
others in
CS_Dept@Sata Technology
a throws clause.
Object Oriented Programming - CoSc 2082 16
College
The finally Block
• The finally block contains code to be executed whether or
not an exception is thrown in a try block.
• The finally block, if used, is placed after a try block and its
following catch blocks.
The general syntax is as follows:
try{
...
}
catch(ExceptionClass1 e){
...
}
catch(ExceptionClassLast e){
...
}
finally{
< Code to be executed whether or not an exception is thrown or caught.>
}
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 17
College
finally block…
• There are three possibilities when the code in the
try-catch-finally blocks is run:
1.The try block runs to the end and no exception is thrown.
• In this case, the finally block is executed after the try block.
2.An exception is thrown in the try block and is caught in
one of the catch blocks positioned after the try block.
• In this case, the finally block is executed after the catch block is
executed.
3.An exception is thrown in the try block and there is no
matching catch block in the method to catch the
exception.
• In this case, the method invocation ends and the exception object
is thrown to the enclosing method. However, the finally block is
executed before the method ends.
CS_Dept@Sata Technology
Object Oriented Programming - CoSc 2082 18
College