Exception Handling
Exception Handling
2/15/2021 2
Performing action in response to exception
Examples
Exit program (abort)
Deal with exception and continue
Print error message
Request new data
Retry action
2/15/2021 3
2/15/2021 4
Java Exception class hierarchy
ClassNotFoundException
CloneNotSupportedException
Exception
IOException
ArithmeticException
AWTException
NullPointerException
RuntimeException
Object Throwable IndexOutOfBoundsException
…
NoSuchElementException
LinkageError
…
VirtualMachoneError
Error
AWTError
Checked
…
Unchecked
2/15/2021 5
Java exception handling is managed by via five
keywords: try, catch, throw, throws, and
finally
Program statements to monitor are contained
within a try block
If an exception occurs within the try block, it is
thrown
Code within catch block catch the exception
and handle it
2/15/2021 6
Output:
Division by zero.
After catch statement.
2/15/2021 7
The scope of a catch clause is restricted to those statements
specified by the immediately preceding try statement.
2/15/2021 8
If more than one occur, then we have to use
multiple catch clauses
When an exception is thrown, each catch
statement is inspected in order, and the first
one whose type matches the exception is
executed
After one catch statement executes, the others
are bypassed
2/15/2021 9
2/15/2021 10
Exception subclass must come before any of their
super classes
2/15/2021 11
2/15/2021 12
It is possible for your program to throw
an exception explicitly
throw ThrowableInstance
Here, ThrowableInstance must be an object
of type Throwable or a subclass Throwable
There are two ways to obtain a Throwable
objects:
Using a parameter into a catch clause
Creating one object with the new operator
2/15/2021 13
Output:
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
2/15/2021 14
If a method doesn’t handle a checked exception, it should be
declared with throws keyword
type method-name parameter-list) throws exception-list
{
// body of method
}
It is not applicable for Error or RuntimeException, or any of
their subclasses
2/15/2021 15
2/15/2021 16
Output:
Inside throwOne.
Caught java.lang.IllegalAccessException: demo
2/15/2021 17
finally creates a block of code that will be executed
after a try/catch block has completed and before the
code following the try/catch block.
finally block will be executed whether or not an
exception is thrown.
Any time a method is about to return to the caller
from inside a try/catch block, via an uncaught
exception or an explicit return statement, the finally
clause is also executed just before the method
returns.
Each try clause requires at least one catch or finally
clause.
2/15/2021 18
2/15/2021 19
inside procA
procA's finally
Exception caught
inside procB
procB's finally
inside procC
procC's finally
2/15/2021 20
A new exception object is
class exc0{
constructed and then thrown.
public static void main(String args[])
{
This exception is caught by the
int d=0;
default handler provided by the
int a=42/d;
java runtime system.
}
}
The default handler displays a
string describing the exception,
prints the stack trace from the
Output: point at which the exception
occurred and terminates the
java.lang.ArithmeticException: / by zero
program.
at exc0.main(exc0.java:4)
2/15/2021 21
Throwable overrides the toString() method (defined by
Object) so that it returns a string containing a description
of the exception.
Example:
catch(ArithmeticException e)
{
System.out.println(“Exception: “+e);
}
Output:
Exception: java.lang.ArithmeticException: / by zero
2/15/2021 22
Define a subclass of the Exception class.
The new subclass inherits all the methods of Exception and can
override them.
2/15/2021 23
class test{
static void compute (int a) throws Myexception{
if(a>10) throw new MyException(a);
System.out.println(“Normal Exit”);
}
public static void main(String args[]){
try{
compute(1);
compute(20);
}catch(MyException e){ System.out.println(“Caught “ +e);
}
}
2/15/2021 24
class InvalidRadiusException extends Exception {
private double r;
public InvalidRadiusException(double radius){
r = radius;
}
public void printError(){
System.out.println("Radius [" + r + "] is not valid");
}
}
2/15/2021 25
class Circle {
double x, y, r;
2/15/2021 26
class CircleTest {
public static void main(String[] args){
try{
Circle c1 = new Circle(10, 10, -1);
System.out.println("Circle created");
}
catch(InvalidRadiusException e)
{
e.printError();
}
}
}
2/15/2021 27