Exception Handling in Java
Exception Handling in Java
An object of
exception exception
Int data=10/0; class is thrown object
No Is Yes
handled
?
JVM
1)Prints out exception Rest of the code is
description executed
2) Prints the stack trace
3) Terminate the program
Exception
An exception is an abnormal condition that arises in a code
sequence at run time.
In other words, an exception is a run-time error
A Java exception is an object that describes an exceptional
(that is, error) condition that has occurred in a piece of code.
When an exceptional condition arises, an object representing
that exception is created and thrown in the method that
caused the error.
That method may choose to handle the exception itself, or
pass it on. Either way, at some point, the exception is caught
and processed.
Try-catch Block
try{
//statements that may cause an exception
}
catch (exception(type) e(object))
{
//error handling code
}
Multiple catch
try { //Protected code
}
catch(ExceptionType1 e1)
{
//Catch block
}
catch(ExceptionType2 e2)
{
//Catch block
}
……
Sequence of Events
Preceding step
try block
statement
unmatched catch
matching catch
unmatched catch
next step
class Example2{
public static void main(String args[]){
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Warning:ArrayIndexOutOfBoundsException"); }
catch(Exception e){
System.out.println("Warning: Some Other exception"); }
try {
statement 2; }
catch(Exception e1) {
//Exception Message }
}
catch(Exception e2) //Catch of Main(parent) try block
{ //Exception Message
}
What is Finally Block
A finally statement must be associated with
a try statement.
Try
{
Try ……
{ }
……. catch(…)
} { ……..
Finally }
{ Finally
……… {
} }
Program Code
Exception Yes
no
Occurred
?
no Yes
Exception
Handled ?
Preceding step
try block
statement
unmatched catch
matching catch
unmatched catch
finally
next step
class Simple{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
} }
Throwing our Own Exceptions
throw keyword
In java we have already defined exception classes such as ArithmeticException, NullPointerException etc.
throw ThrowableInstance;
Primitive types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used
as exceptions.
Syntax of throw statement
throw is used within the method. throws is used with the method
signature.
You cannot throw multiple You can declare multiple exception
exception e.g.
public void method()throws
IOException,SQLException.
Java’s Built-in Exceptions: Unchecked
Inside the standard package
However, the actual cause of the problem was that an I/O error occurred, which caused the
Although the method must certainly throw an ArithmeticException, since that is the error that
occurred, you might also want to let the calling code know that the underlying cause was an
I/O error.
Chained exceptions let you handle this, and any other situation in which layers of exceptions
exist.
// Demonstrate exception
chaining.
class ChainExcDemo {
static void demoproc() {
// create an exception
NullPointerException e = new
NullPointerException("top
layer");
// add a cause
e.initCause(new The output from the program is shown here:
Caught: java.lang.NullPointerException: top layer
ArithmeticException("cause"));
Original cause: java.lang.ArithmeticException: cause