Dr. JALESH KUMAR. Module 4 CSE
and by
Exception is of two types:
1. Unchecked Exception
2. Checked Exception
1. Unchecked Exceptions:
The exceptions which are not checked by compiler
* Cony
vr do not produce any error whether
ou handle or not
* But when program will be executed the exception occurs
Examples:
+ ArithmeticException
+ ArrayIndexOutOfBoundException
+ NoliPointerException
Scanned with CamScannerDr. JALESH KUMAR Module 4 CSE
2. Checked Exceptions:
+ Exception which are checked by compiler at runtime
+ Compiler time error if it is not handled
+ We have to use try catch, throws such exception handlers to
handle checked exceptions
Examples:
+ ClassNotFoundExcept
+ InterruptedException
+ IMlegalAccessExcepti
Creating our own exceptions:
Itis possible to c1
te our own exception instead of using built in ex
tions
The class should extend built in Exception class. In the below example, class A
extends Exception, Now we can consider A as exception class. Hence, in class
B, method m1() will throws the exception A. The caller of the method m1()
should write the method m1() inside try-catch block. Catch should be used with
exception A as in example.
Creating your class 8
own Exception { 2 ievoid mi() throws A
throw new A(]
}
publicstatic void main(String s{])
( try
rms
Jeateh(Aey
Systemou rinse,
et
)
Scanned with CamScannerDr. JALESH KUMAR Module 4 cse
Chained Exceptions
* The chained exception feature allows to associate another exception
with an exception
* Second exception describe the cause of first exception
* EX: if a method thrown an ArithmeticException because of divide by
zero. However, actual cause was an |/O error
+ initCause() and getCause() methods can be used
Scanned with CamScanner