Chapter8 Exception Handling
Chapter8 Exception Handling
Exception Handling in
Java
Types of Errors
1.Compile time
All syntax errors identified by java
compiler.
No class file is created when this occurs.
So it is necessary to fix all compile time
errors for successful compilation.
Egs:
Missing of semicolon,
use of = instead of ==
Exception Handling in
Java
2.Run time
Some pgms may not run successfully due
to wrong logic or errors like stack
overflow.
Some of the Common run time errors are:
Division by 0
Array index out of bounds
Negative array size etc..
3
Exception Handling in
Java
Exception is a condition caused by a run time
error in the program. When the java interpreter
identifies an error such as division by 0 it
creates an Exception object and throws it
Definition:
An exception is an event, which occurs during
the execution of a program, that disrupts the
normal flow of the program's instructions.
Is defined as a condition that interrupts the
normal flow of operation within a program.
4
Exception Handling in
Java
This mechanism consists of :
1. Find the problem(Hit the Exception)
2. Inform that an error has occurred(Throw
the Exception)
3. Receive the error Information(Catch the
Exception)
4. Take corrective actions(Handle the
Exception)
Exception Handling in
Java
In Java Exception handling is managed by 5
key words:
try
catch
throw
throws
finally
Exception
Hierarchy
Package java.lang
Throwable
Exception
InterruptedException
(checked exception)
error
RuntimeException
(unchecked exception)
Meaning
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
ClassCastException
Invalid cast
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IndexOutOfBoundsException
NegativeArraySizeException
Meaning
NullPointerException
NumberFormatException
SecurityException
StringIndexOutOfBoundsException
TypeNotPresentException
UnsupportedOperationException
11
Meaning
ClassNotFoundException
CloneNotSupportedException
IllegalAccessException
InstantiationException
InterruptedException
NoSuchFieldException
NoSuchMethodException
12
13
Exception Handling in
Java
try Block
Statement that
causes Exception
Catch Block
Statement that
causes Exception
14
Exception Handling in
Java
try{
Statement:
}
catch(Exception-type e){
statement;
}
15
Exception Handling in
Java
class Ex{
public static void main(String args[]){
int d,a;
try{
d=0;
a=10/d;
System.out.println("from try");
}catch(ArithmeticException e)
{
System.out.println("divsn by Zero");
}
System.out.println("after catch");
}
}
Once an exception is
thrown , program control
transfers out of the try
block into the catch block.
Once the catch statement
is executed pgm control
continues with the next
line following the entire
try/catch mechanism.
16
Exception Handling in
Java
17
error message.
18
catch(ArithmeticException e){
System.out.println(e.getMessage());
//e.printStackTrace();
}
o/p:E:\JAVAPGMS>java Ex
/ by zero
catch(ArithmeticException e){
e.printStackTrace();
}
E:\JAVAPGMS>java Ex
o/p:
java.lang.ArithmeticException: / by zero
at Ex.main(Ex.java:9)
19
20
Exception Handling in
Java
class Ex{
public static void main(String
args[]){
int d,a,len;
try{
len=args.length;
a=10/len;
int c[]={1};
catch(ArithmeticException e){
c[10]=23;
System.out.println("divsn by
}
Zero"+e);
}
catch(ArrayIndexOutOfBoundsExcept
ion ae){
System.out.println("Array index"+ae);
}
System.out.println("after catch");
}
}
21
Exception Handling in
Java
In multiple catch statement exception subclasses
must come before any of their superclasses.
Because a catch statement that uses a
superclass will catch exception of that type plus
any of its subclasses.
Thus a subclass would never be reached if it
came after its superclass.
22
Exception Handling in
Nested tryJava
statement
try statement can be nested
class Ex{
public static void main(String dd[]){
int d,a,len;
try{
len=dd.length;
a=10/len;
System.out.println(a);
catch(ArrayIndexOutOfBoundsExceptio
try{
n ae){
if(len==1)
System.out.println("Array index"+ae);
len=len/(len-len);
}
if(len==2){
}
int c[]={1};
catch(ArithmeticException e){
c[10]=23;
e.printStackTrace();
}
}
}
}
System.out.println("after catch");
}
}
23
at Ex.main(Ex.java:9)
after catch
E:\JAVAPGMS>java Ex 20
10
java.lang.ArithmeticException: / by zero
at Ex.main(Ex.java:14)
after catch
E:\JAVAPGMS>java Ex 20 30
5
Array indexjava.lang.ArrayIndexOutOfBoundsException: 10
after catch
24
Exception Handling in
Java
throw
It is possible to throw an exception explicitly.
Syntax:
throw ThrowableInstance
25
Exception Handling in
Java
class throwDemo{
public static void main(String args[]){
int size;
int arry[]=new int[3];
size=Integer.parseInt(args[0]);
try{
if(size<=0)throw new
NegativeArraySizeException("Illegal
Array size");
for(int i=0;i<3;i++)
arry[i]+=i+1;
}catch(NegativeArraySizeException e){
System.out.println(e);
}
}
}
26
Exception Handling in
Java
E:\JAVAPGMS>java throwDemo -4
java.lang.NegativeArraySizeException:
Illegal Array size
Exception in thread "main"
java.lang.NegativeArraySizeException:
Illegal Array size
at
throwDemo.main(throwDemo.java:17)
27
throws
Exception Handling in
Java
28
Exception Handling in
Java
import java.io.*;
class ThrowsDemo{
public static void main(String args[])throws
IOException,NumberFormatException{
int i;
InputStreamReader in=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
i=Integer.parseInt(br.readLine());
System.out.println(i);
}}
29
finally
Exception Handling in
Java
Exception Handling in
Java
Form:
try
try block
{}
catch(exceptiontype
e)
{}
finally
Catch block
finally
{}
finally
31
Exception Handling in
Creating Java
our own Exception class
For creating an exception class our own simply
make our class as subclass of the super class
Exception.
Eg:
class MyException extends Exception{
MyException(String msg){
super(msg);
}
}
32
34