Exception Handling
Exception Handling
Unit-06
Exception
Handling
Exception
Using try and catch
Nested try statements
The Exception Class Hierarchy
throw statement
throws statement
Exceptions
An exception is an object that describes an unusual or erroneous situation.
Exceptions are thrown by a program, and may be caught and handled by another part of the
program.
A program can be separated into a normal execution flow and an exception execution flow.
An error is also represented as an object in Java, but usually represents a unrecoverable
situation and should not be caught.
Java has a predefined set of exceptions and errors that can occur during execution.
A program can deal with an exception in one of three ways:
ignore it
handle it where it occurs
handle it at another place in the program
The manner in which an exception is processed is an important design consideration.
Multiple catch:
try{
// code that may cause exception
}
catch(ArithmeticException ae){
// code when arithmetic exception occurred
}
catch(ArrayIndexOutOfBoundsException aiobe){
// when array index out of bound exception occurred
}
Exception Meaning
ClassNotFoundException Class not found.
IOException Input Output Exceptions
CloneNotSupportedException Attempt to clone an object that does not implement the Cloneable interface.
IllegalAccessException Access to a class is denied.
InstantiationException Attempt to create an object of an abstract class or interface.
InterruptedException One thread has been interrupted by another thread.
NoSuchFieldException A requested field does not exist.
NoSuchMethodException A requested method does not exist.