Exception Handling
Exception Handling
Example Program:
class NoExceptionHandling
{
public static void main(String args[])
{
int a=10,b=20,c=0;
System.out.println("Before");
a=b+c;
a=b/c;
System.out.println("After");
}
}
UNIT-III 3
Exception Handling Fundamentals:
UNIT-III 4
Exception Handling Fundamentals:
Exceptions are built into the Java language and are available to all program
code.
UNIT-III 5
Exception Handling Fundamentals:
Java exception handling is managed by five keywords:
try
catch
throw
throws
finally
UNIT-III 9
Nested try Statements:
In the latter case, the Java run-time system will handle the exception.
UNIT-III 10
Example program: Nested try
class NestedTryDemo1{
public static void main(String args[ ]) {
int a=10,b=20,c=0;
System.out.println("Before try");
try { Output:-
a=b+c;
try {
a=b/c;
System.out.println("In inner try");
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception caught");
}
System.out.println("In outer try");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("AIOOB Exception caught");
}
System.out.println("After try"); UNIT-III 11
}}
Example program: Nested try
class NestedTryDemo2{
public static void main(String args[ ]) {
int a=10,b=20,c=0;
System.out.println("Before try");
try { Output:-
a=b+c;
try {
a=b/c;
System.out.println("In inner try");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(" AIOOB Exception caught");
}
System.out.println("In outer try");
}
catch(ArithmeticException e)
{
System.out.println(" Arithmetic Exception caught");
}
System.out.println("After try"); UNIT-III 12
}}
Exception Sources:
UNIT-III 13
Throwing Exceptions:
So far, we were only catching the exceptions thrown by the Java system.
throw ThrowableInstance;
try
{
throw new ArithmeticException("demo");
}
catch(ArithmeticException e)
{
System.out.println("Exception“+e);
}
try try
{ {
throw new NullPointerException("demo"); throw eObj;
} }
catch(NullPointerException e) catch(Exception e)
{ {
System.out.println("Exception“+e); System.out.println("Exception“+e);
} }
UNIT-III 15
finally Clause:
The try statement requires at least one catch or finally clause, although both
are optional:
try { … }
catch(Exception1 ex1) { … } …
finally { … }
Any time a method is to return to a caller from inside the try/catch block via:
1) uncaught exception or
2) explicit return
UNIT-III 16
Example program: finally
void procA( ) { void procB( ) {
try { try {
System.out.println("inside procA"); System.out.println("inside procB");
throw new RuntimeException("demo"); return;
} }
finally { finally {
System.out.println("procA's finally"); System.out.println("procB's finally");
} }
} }
Object
Throwable
Error Exception
RunTimeException
LinkageError ArithmeticException
IndexOutOfBoundsException
ThreadDeath StringIndexOutOfBoundsException
IllegalArguementException
VirtualMachineError NumberFormatException
IllegalAccessException
AWTError NoSuchMethodException
UNIT-III
ClassNotFoundException18
Java Exception class hierarchy:
ClassNotFoundException
CloneNotSupportedException
Exception
IOException
ArithmeticException
AWTException
NullPointerException
RuntimeException
Object Throwable IndexOutOfBoundsException
…
NoSuchElementException
LinkageError
…
VirtualMachoneError
Error
AWTError
Checked
…
Unchecked
UNIT-III 19
Exception Types:
UNIT-III 20
Unchecked Built-In Exceptions:
UNIT-III 21
Unchecked Built-In Exceptions:
UNIT-III 22
Checked Built-In Exceptions:
UNIT-III 23
throws:
UNIT-III 24
throws: Example program1
class ThrowsDemo {
void m1( ) {
System.out.println("Inside m1.");
throw new IllegalAccessException("demo");
}
void m2( ) {
System.out.println("Inside m2.");
throw new ArithmeticException("demo");
}
public static void main(String args[ ]) {
ThrowsDemo t1=new ThrowsDemo( );
t1.m1( );
t1.m2( );
}
}
UNIT-III 25
throws: Example program1
UNIT-III 26
throws: Example program2
class ThrowsDemo1 {
void m1( ) throws IllegalAccessException {
System.out.println("Inside m1.");
throw new IllegalAccessException("demo");
}
void m2( ) {
System.out.println("Inside m2.");
throw new ArithmeticException("demo");
}
public static void main(String args[ ]) {
ThrowsDemo1 t1=new ThrowsDemo1( );
t1.m1( );
t1.m2( );
}
}
UNIT-III 27
throws: Example program2
UNIT-III 28
throws: Example program3
class ThrowsDemo2 {
void m1( ) throws IllegalAccessException {
System.out.println("Inside m1.");
throw new IllegalAccessException("demo");
}
void m2( ) {
System.out.println("Inside m2.");
throw new ArithmeticException("demo");
}
public static void main(String args[ ]) {
ThrowsDemo2 t1=new ThrowsDemo2( );
try{
t1.m1( );
}
catch(IllegalAccessException e)
{
System.out.println("Exception caught");
}
t1.m2( );
}
}
UNIT-III 29
throws: Example program3
UNIT-III 30
throws: Example program4
class ThrowsDemo3 {
void m1( ) throws IllegalAccessException {
System.out.println("Inside m1.");
throw new IllegalAccessException("demo");
}
void m2( ) {
System.out.println("Inside m2.");
throw new ArithmeticException("demo");
}
public static void main(String args[ ]) throws IllegalAccessException
{
ThrowsDemo3 t1=new ThrowsDemo3( );
t1.m1( );
t1.m2( );
}
}
UNIT-III 31
Creating Own Exception Classes:
UNIT-III 32
Creating Own Exception Classes: Example program1
class RangeException extends Exception {
String name;
RangeException(String name1) {
name=name1;
}
public String toString( ) {
return "Exception:"+name+"RangeException";
}
}
class ExceptionDemo {
public static void main(String args[ ]) {
int i=10;
try {
if(i>=10)
throw new RangeException("first");
}
catch(RangeException e) {
System.out.println(e);
}
}
} UNIT-III 33
ChainedExceptions:
Constructors:
Throwable(Throwable causeExc)
Throwable(String msg, Throwable causeExc)
Methods:
Throwable getCause( )
Throwable initCause(Throwable causeExc)
UNIT-V 34
UNIT-III 34
ChainedExceptions: Example program1
class ChainedExceptionsDemo
{
public static void main(String args[])
{
ArithmeticException e=new ArithmeticException();
ArrayIndexOutOfBoundsException a1=new
ArrayIndexOutOfBoundsException();
IllegalAccessException i1=new IllegalAccessException();
e.initCause(a1);
a1.initCause(i1);
try
{
throw e;
}
catch(ArithmeticException ae)
{
System.out.println(ae);
System.out.println(ae.getCause());
System.out.println(ae.getCause().getCause());
}
}
} UNIT-III 35