Exception Handling in Java
Dr. S Sudheer Mangalampalli
Assistant Professor, Senior Grade1
School of Computer Science and Engineering
VIT-AP University
May 14, 2022
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
1 / 47
Intro about Exception Handling
The Exception Handling in Java is one of the powerful mechanism to
handle the runtime errors so that the normal flow of the application
can be maintained.
What is Exception in Java? In Java, an exception is an event that
disrupts the normal flow of the program. It is an object which is
thrown at runtime.
What is Exception Handling? Exception Handling is a mechanism
to handle runtime errors such as ClassNotFoundException,
IOException, SQLException, RemoteException, etc.
Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow
of the application.
An exception normally disrupts the normal flow of the application; that is
why we need to handle exceptions.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
2 / 47
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;// exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there are 10 statements in a Java program and an exception
occurs at statement 5; the rest of the code will not be executed, i.e.,
statements 6 to 10 will not be executed. However, when we perform
exception handling, the rest of the statements will be executed. That is
why we use exception handling in Java.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
3 / 47
Hierarchy of Java Exception Classes
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
4 / 47
Types of Java Exceptions
The [Link] class is the root class of Java Exception hierarchy
inherited by two subclasses: Exception and Error.
There are mainly two types of exceptions: checked and unchecked. An
error is considered as the unchecked exception. However, according to
Oracle, there are three types of exceptions namely:
Checked Exception
Unchecked Exception
Error
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
5 / 47
Difference between Checked and Unchecked Exceptions
Checked Exception
The classes that directly inherit the Throwable class except RuntimeExcep-
tion and Error are known as checked exceptions. For example, IOException,
SQLException, etc. Checked exceptions are checked at compile-time.
Unchecked Exception
The classes that inherit the RuntimeException are known as unchecked
exceptions. For example, ArithmeticException, NullPointerException, Ar-
rayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked
at compile-time, but they are checked at runtime.
Error
Error is irrecoverable. Some example of errors are OutOfMemoryError, Vir-
tualMachineError, AssertionError etc.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
6 / 47
How JVM handles Exception?
Whenever inside a method, if an exception has occurred, the method
creates an Object known as an Exception Object and hands it off to
the run-time system(JVM).
The exception object contains the name and description of the
exception and the current state of the program where the exception
has occurred.
Creating the Exception Object and handling it in the run-time system
is called throwing an Exception.
There might be a list of the methods that had been called to get to
the method where an exception [Link] ordered list of the
methods is called Call Stack.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
7 / 47
The run-time system searches the call stack to find the method that
contains a block of code that can handle the occurred exception. The
block of the code is called an Exception handler.
The run-time system starts searching from the method in which the
exception occurred, and proceeds through the call stack in the reverse
order in which methods were called.
If it finds an appropriate handler then it passes the occurred exception
to it. An appropriate handler means the type of the exception object
thrown matches the type of the exception object it can handle.
If the run-time system searches all the methods on the call stack and
couldn’t have found the appropriate handler then the run-time system
handover the Exception Object to the default exception handler,
which is part of the run-time system. This handler prints the
exception information in the following format and terminates the
program abnormally.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
8 / 47
Java try-catch block
Java try block is used to enclose the code that might throw an
exception. It must be used within the method.
If an exception occurs at the particular statement in the try block, the
rest of the block code will not execute. So, it is recommended not to
keep the code in try block that will not throw an exception.
Java try block must be followed by either catch or finally block.
Syntax of try-catch block
try {
// code t h a t may th ro w an e x c e p t i o n
} catch ( E x c e p t i o n c l a s s N a m e r e f ){}
Syntax of try-finally block
try {
// code t h a t may thro w an e x c e p t i o n
} f i n a l l y {}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
MayVIT-AP
14, 2022University
9 / 47
Java Catch block
Java catch block is used to handle the Exception by declaring the
type of exception within the parameter.
The declared exception must be the parent class exception ( i.e.,
Exception) or the generated exception type.
The good approach is to declare the generated type of exception.
The catch block must be used after the try block only. You can use
multiple catch block with a single try block.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
10 / 47
Internal working of Java try-catch block
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
11 / 47
Example1
p u b l i c c l a s s TryCatchExample1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t d a t a =50/0; //may th ro w e x c e p t i o n
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
12 / 47
Example2
p u b l i c c l a s s TryCatchExample2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try
{
i n t d a t a =50/0; //may th ro w e x c e p t i o n
}
// h a n d l i n g t h e e x c e p t i o n
catch ( ArithmeticException e )
{
System . o u t . p r i n t l n ( e ) ;
}
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
} }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
13 / 47
Example3
p u b l i c c l a s s TryCatchExample3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try
{
i n t d a t a =50/0; //may th ro w e x c e p t i o n
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}
// h a n d l i n g t h e e x c e p t i o n
catch ( ArithmeticException e )
{
System . o u t . p r i n t l n ( e ) ;
}
} }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
14 / 47
Example4
p u b l i c c l a s s TryCatchExample4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try
{
i n t d a t a =50/0; //may th ro w e x c e p t i o n
}
// h a n d l i n g t h e e x c e p t i o n
catch ( Exception e )
{
System . o u t . p r i n t l n ( e ) ;
}
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
15 / 47
Example5
p u b l i c c l a s s TryCatchExample5 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try
{
i n t d a t a =50/0; //may th ro w e x c e p t i o n
}
// h a n d l i n g t h e e x c e p t i o n
catch ( Exception e )
{
System . o u t . p r i n t l n ( ” I t c a n n o t be d i v i d e d by z e r o ” ) ;
}
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
16 / 47
Example6
p u b l i c c l a s s TryCatchExample6 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t i =50;
i n t j =0;
i n t data ;
try
{
d a t a=i / j ; //may t hr ow e x c e p t i o n
}
// h a n d l i n g t h e e x c e p t i o n
catch ( Exception e )
{
// r e s o l v i n g t h e e x c e p t i o n i n c a t c h b l o c k
System . o u t . p r i n t l n ( i / ( j + 2 ) ) ;
}
} }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
17 / 47
Example7
p u b l i c c l a s s TryCatchExample7 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try
{
i n t d a t a =50/0; //may th ro w e x c e p t i o n
}
// h a n d l i n g t h e e x c e p t i o n
catch ( ArrayIndexOutOfBoundsException e )
{
System . o u t . p r i n t l n ( e ) ;
}
System . o u t . p r i n t l n ( ” R e s t o f t h e code ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
18 / 47
Java Multi-catch Block
A try block can be followed by one or more catch blocks.
Each catch block must contain a different exception handler. So, if
you have to perform different tasks at the occurrence of different
exceptions, use java multi-catch block.
At a time only one exception occurs and at a time only one catch
block is executed.
All catch blocks must be ordered from most specific to most general,
i.e. catch for ArithmeticException must come before catch for
Exception.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
19 / 47
Flowchart of Multi-catch Block
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
20 / 47
Example1
public c l a s s MultipleCatchBlock1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
i n t a [ ] = new i n t [ 5 ] ;
a [5]=30/0;
}
catch ( ArithmeticException e )
{ System . o u t . p r i n t l n ( ” A r i t h m e t i c E x c e p t i o n o c c u r s ” ) ;
}
catch ( ArrayIndexOutOfBoundsException e )
{ System . o u t . p r i n t l n ( ” A r r a y I n d e x O u t O f B o u n d s ” ) ;
}
catch ( Exception e )
{ System . o u t . p r i n t l n ( ” P a r e n t E x c e p t i o n o c c u r s ” ) ;
}
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
21 / 47
Example2
public c l a s s MultipleCatchBlock2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
i n t a [ ] = new i n t [ 5 ] ;
System . o u t . p r i n t l n ( a [ 1 0 ] ) ;
}
catch ( ArithmeticException e )
{ System . o u t . p r i n t l n ( ” A r i t h m e t i c E x c e p t i o n o c c u r s ” ) ;
}
catch ( ArrayIndexOutOfBoundsException e )
{ System . o u t . p r i n t l n ( ” A r r a y I n d e x O u t O f B o u n d s ” ) ;
}
catch ( Exception e )
{ System . o u t . p r i n t l n ( ” P a r e n t E x c e p t i o n o c c u r s ” ) ;
}
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
22 / 47
Example3
public c l a s s MultipleCatchBlock3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
i n t a [ ] = new i n t [ 5 ] ;
a [5]=30/0;
System . o u t . p r i n t l n ( a [ 1 0 ] ) ;
}
catch ( ArithmeticException e )
{ System . o u t . p r i n t l n ( ” A r i t h m e t i c E x c e p t i o n o c c u r s ” ) ;
}
catch ( ArrayIndexOutOfBoundsException e )
{ System . o u t . p r i n t l n ( ” A r r a y I n d e x O u t O f B o u n d s ” ) ; }
catch ( Exception e )
{ System . o u t . p r i n t l n ( ” P a r e n t E x c e p t i o n o c c u r s ” ) ; }
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
23 / 47
Example4
public c l a s s MultipleCatchBlock3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
String s = null ;
System . o u t . p r i n t l n ( s . l e n g t h ( ) ) ;
}
catch ( ArithmeticException e )
{ System . o u t . p r i n t l n ( ” A r i t h m e t i c E x c e p t i o n o c c u r s ” ) ;
}
catch ( ArrayIndexOutOfBoundsException e )
{ System . o u t . p r i n t l n ( ” A r r a y I n d e x O u t O f B o u n d s ” ) ; }
catch ( Exception e )
{ System . o u t . p r i n t l n ( ” P a r e n t E x c e p t i o n o c c u r s ” ) ; }
System . o u t . p r i n t l n ( ” r e s t o f t h e code ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
24 / 47
Nested try block
In Java, using a try block inside another try block is permitted. It is
called as nested try block.
Why use nested try block Sometimes a situation may arise where a part
of a block may cause one error and the entire block itself may cause
another error. In such cases, exception handlers have to be nested.
// main t r y b l o c k
try
{
statement 1;
statement 2;
// t r y c a t c h b l o c k w i t h i n a n o t h e r t r y b l o c k
try
{
statement 3;
statement 4;
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
25 / 47
// t r y c a t c h b l o c k w i t h i n n e s t e d t r y b l o c k
try
{
statement 5;
statement 6;
}
c a t c h ( E x c e p t i o n e2 )
{
// e x c e p t i o n message
}}
c a t c h ( E x c e p t i o n e1 )
{
// e x c e p t i o n message
} }
// c a t c h b l o c k o f p a r e n t ( o u t e r ) t r y b l o c k
c a t c h ( E x c e p t i o n e3 )
{
// e x c e p t i o n message
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
26 / 47
Java Nested try block
When any try block does not have a catch block for a particular
exception, then the catch block of the outer (parent) try block are
checked for that exception, and if it matches, the catch block of outer
try block is executed.
If none of the catch block specified in the code is unable to handle the
exception, then the Java runtime system will handle the exception.
Then it displays the system generated message for that exception.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
27 / 47
Example1
p u b l i c c l a s s NestedTryBlock {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
// o u t e r t r y b l o c k
try {
// i n n e r t r y b l o c k 1
try {
System . o u t . p r i n t l n ( ” g o i n g t o d i v i d e by 0 ” ) ;
i n t b =39/0;
}
// c a t c h b l o c k o f i n n e r t r y b l o c k 1
catch ( ArithmeticException e )
{
System . o u t . p r i n t l n ( e ) ;
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
28 / 47
Example1
// i n n e r t r y b l o c k 2
try {
i n t a [ ] = new i n t [ 5 ] ;
// a s s i g n i n g t h e v a l u e o u t o f a r r a y bounds
a [5]=4;
}
// c a t c h b l o c k o f i n n e r t r y b l o c k 2
catch ( ArrayIndexOutOfBoundsException e )
{
System . o u t . p r i n t l n ( e ) ;
}
System . o u t . p r i n t l n ( ” o t h e r s t a t e m e n t ” ) ;
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
29 / 47
// c a t c h b l o c k o f o u t e r t r y b l o c k
catch ( Exception e )
{
System . o u t . p r i n t l n ( ” h a n d l e d t h e e x c e p t i o n ( o u t e r c
}
System . o u t . p r i n t l n ( ” n o r m a l f l o w . . ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
30 / 47
Example2
p u b l i c c l a s s NestedTryBlock2 {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
// o u t e r ( main ) t r y b l o c k
try {
// i n n e r t r y b l o c k 1
try {
// i n n e r t r y b l o c k 2
try {
int arr [ ] = { 1 , 2 , 3 , 4 };
// p r i n t i n g t h e a r r a y e l e m e n t o u t o f i t s bounds
System . o u t . p r i n t l n ( a r r [ 1 0 ] ) ; }
// t o h a n d l e s A r i t h m e t i c E x c e p t i o n
catch ( ArithmeticException e ) {
System . o u t . p r i n t l n ( ” A r i t h m e t i c e x c e p t i o n ” ) ;
System . o u t . p r i n t l n ( ” i n n e r t r y b l o c k 2 ” ) ;
} }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
31 / 47
// t o h a n d l e A r i t h m e t i c E x c e p t i o n
catch ( ArithmeticException e ) {
System . o u t . p r i n t l n ( ” A r i t h m e t i c e x c e p t i o n ” ) ;
System . o u t . p r i n t l n ( ” i n n e r t r y b l o c k 1 ” ) ;
}}
// t o h a n d l e A r r a y I n d e x O u t O f B o u n d s E x c e p t i o n
c a t c h ( A r r a y I n d e x O u t O f B o u n d s E x c e p t i o n e4 ) {
System . o u t . p r i n t ( e4 ) ;
System . o u t . p r i n t l n ( ” o u t e r ( main ) t r y b l o c k ” ) ;
}
c a t c h ( E x c e p t i o n e5 ) {
System . o u t . p r i n t ( ” E x c e p t i o n ” ) ;
System . o u t . p r i n t l n ( ” h a n d l e d i n main t r y −b l o c k ” ) ;
}}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
32 / 47
Java Finally Block
Java finally block is a block used to execute important code such as
closing the connection, etc.
Java finally block is always executed whether an exception is handled
or not. Therefore, it contains all the necessary statements that need
to be printed regardless of the exception occurs or not.
The finally block follows the try-catch block.
Why use Java finally block? finally block in Java can be used to
put ”cleanup” code such as closing a file, closing connection, etc.
The important statements to be printed can be placed in the finally
block.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
33 / 47
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
34 / 47
Example1
class TestFinallyBlock {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
try {
// b e l o w code do n o t t hr o w any e x c e p t i o n
i n t d a t a =25/5;
System . o u t . p r i n t l n ( d a t a ) ; }
// c a t c h won ’ t be e x e c u t e d
catch ( N u l l P o i n t e r E x c e p t i o n e ){
System . o u t . p r i n t l n ( e ) ; }
// e x e c u t e d r e g a r d l e s s o f e x c e p t i o n o c c u r r e d o r n o t
finally {
System . o u t . p r i n t l n ( ” f i n a l l y b l o c k i s a l w a y s e x e c u t e d ” )
System . o u t . p r i n t l n ( ” r e s t o f phe code . . . ” ) ; }}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
35 / 47
Example2
public class TestFinallyBlock1 {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
try {
System . o u t . p r i n t l n ( ” I n s i d e t h e t r y b l o c k ” ) ;
// b e l o w code t h r o w s d i v i d e by z e r o e x c e p t i o n
i n t d a t a =25/0;
System . o u t . p r i n t l n ( d a t a ) ; }
// c a n n o t h a n d l e A r i t h m e t i c t y p e e x c e p t i o n
// can o n l y a c c e p t N u l l P o i n t e r t y p e e x c e p t i o n
catch ( N u l l P o i n t e r E x c e p t i o n e ){
System . o u t . p r i n t l n ( e ) ; }
// e x e c u t e s r e g a r d l e s s o f e x c e p t i o n o c c u r e d o r n o t
finally {
System . o u t . p r i n t l n ( ” f i n a l l y b l o c k i s a l w a y s e x e c u t e d ” )
System . o u t . p r i n t l n ( ” r e s t o f t h e code . . . ” ) ;
} }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
36 / 47
Example3
public class TestFinallyBlock2 {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
try {
System . o u t . p r i n t l n ( ” I n s i d e t r y b l o c k ” ) ;
// b e l o w code t h r o w s d i v i d e by z e r o e x c e p t i o n
i n t d a t a =25/0;
System . o u t . p r i n t l n ( d a t a ) ; }
// h a n d l e s t h e A r i t h m e t i c E x c e p t i o n / D i v i d e by z e r o
catch ( A r i t h m e t i c E x c e p t i o n e ){
System . o u t . p r i n t l n ( ” E x c e p t i o n h a n d l e d ” ) ;
System . o u t . p r i n t l n ( e ) ;
}
// e x e c u t e s r e g a r d l e s s o f e x c e p t i o n o c c u r e d o r n o t
finally {
System . o u t . p r i n t l n ( ” f i n a l l y b l o c k i s a l w a y s e x e c u t e d ” )
System . o u t . p r i n t l n ( ” r e s t o f t h e code . . . ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
37 / 47
Throw key word
The Java throw keyword is used to throw an exception explicitly.
We specify the exception object which is to be thrown. The Exception
has some message with it that provides the error description.
We can throw either checked or unchecked exceptions in Java by
throw keyword. It is mainly used to throw a custom exception.
We can also define our own set of conditions and throw an exception
explicitly using throw keyword.
For example, we can throw ArithmeticException if we divide a
number by another number.
Here, we just need to set the condition and throw exception using
throw keyword.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
38 / 47
Java Throw Keyword
thr ow i n s t a n c e ;
Example
thr ow new I O E x c e p t i o n ( ” s o r r y d e v i c e e r r o r ” ) ;
Where the Instance must be of type Throwable or subclass of Throwable.
For example, Exception is the sub class of Throwable and the user-defined
exceptions usually extend the Exception class.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
39 / 47
Example1
p u b l i c c l a s s TestThrow1 {
// f u n c t i o n t o c h e c k i f p e r s o n i s e l i g i b l e t o v o t e o r n
p u b l i c s t a t i c v o i d v a l i d a t e ( i n t age ) {
i f ( age <18) {
// th r ow A r i t h m e t i c e x c e p t i o n i f n o t e l i g i b l e t o v o t e
thr ow new A r i t h m e t i c E x c e p t i o n ( ” P e r s o n i s n o t e l i g i b l e
e l s e { System . o u t . p r i n t l n ( ” P e r s o n i s e l i g i b l e t o v o t e !
}}
// main method
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
// c a l l i n g t h e f u n c t i o n
validate (13);
System . o u t . p r i n t l n ( ” r e s t o f t h e code . . . ” ) ; }}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
40 / 47
Example2
import java . io . ∗ ;
p u b l i c c l a s s TestThrow2 {
p u b l i c s t a t i c v o i d method ( ) t h r o w s F i l e N o t F o u n d E x c e p t i
F i l e R e a d e r f i l e = new F i l e R e a d e r ( ”C : \ \ U s e r s \\DELL E744
B u f f e r e d R e a d e r f i l e I n p u t = new B u f f e r e d R e a d e r ( f i l e ) ;
t hr ow new F i l e N o t F o u n d E x c e p t i o n ( ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
try
{ method ( ) ; }
catch ( FileNotFoundException e ) {
e . printStackTrace ( ) ; }
System . o u t . p r i n t l n ( ” r e s t o f t h e code . . . ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
41 / 47
Example3
// c l a s s r e p r e s e n t s u s e r −d e f i n e d e x c e p t i o n
c l a s s UserDefinedException extends Exception
{ public UserDefinedException ( String s t r )
{
// C a l l i n g c o n s t r u c t o r o f p a r e n t E x c e p t i o n
s u p e r ( s t r ) ; }}
// C l a s s t h a t u s e s a bo ve MyException
p u b l i c c l a s s TestThrow3
{ p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
try
{
// th ro w an o b j e c t o f u s e r d e f i n e d e x c e p t i o n
t hr ow new U s e r D e f i n e d E x c e p t i o n ( ” T h i s i s u s e r −d e f i n e d
}
c a t c h ( U s e r D e f i n e d E x c e p t i o n ude ) {
System . o u t . p r i n t l n ( ” Caught t h e e x c e p t i o n ” ) ;
System . o u t . p r i n t l n ( ude
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
. g e tSchool
Exception
M e sofinsComputer
Grade1Handling
a g e ( Science
Java
) ) ; and Engineering
May 14,
VIT-AP
2022 University
42 / 47
Exception Propagation
An exception is first thrown from the top of the stack and if it is not
caught, it drops down the call stack to the previous method. If not caught
there, the exception again drops down to the previous method, and so on
until they are caught or until they reach the very bottom of the call stack.
This is called exception propagation.
c l a s s TestExceptionPropagation1 {
v o i d m( ) {
i n t d a t a =50/0;
}
void n (){
m( ) ;
}
void p (){
try {
n(); }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
43 / 47
c a t c h ( E x c e p t i o n e ) { System . o u t . p r i n t l n ( ” e x c e p t i o n h a n d l
} }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
T e s t E x c e p t i o n P r o p a g a t i o n 1 o b j=new T e s t E x c e p t i o n P r o p
obj . p ( ) ;
System . o u t . p r i n t l n ( ” n o r m a l f l o w . . . ” ) ;
} }
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
44 / 47
Exception Propagation
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
45 / 47
Exception Propagation
c l a s s TestExceptionPropagation2 {
v o i d m( ) {
t hr ow new j a v a . i o . I O E x c e p t i o n ( ” d e v i c e e r r o r ” ) ; / / c h
}
void n (){
m( ) ; }
void p (){
try {
n();}
c a t c h ( E x c e p t i o n e ) { System . o u t . p r i n t l n ( ” e x c e p t i o n h a n d e
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
T e s t E x c e p t i o n P r o p a g a t i o n 2 o b j=new T e s t E x c e p t i o n P r o p a g a
obj . p ( ) ;
System . o u t . p r i n t l n ( ” n o r m a l f l o w ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
46 / 47
Java throws keyword
The Java throws keyword is used to declare an exception. It gives an
information to the programmer that there may occur an exception.
So, it is better for the programmer to provide the exception handling
code so that the normal flow of the program can be maintained.
Exception Handling is mainly used to handle the checked exceptions.
If there occurs any unchecked exception such as
NullPointerException, it is programmers’ fault that he is not checking
the code before it being used.
•
Dr. S Sudheer Mangalampalli Assistant Professor, Senior
Exception
Grade1Handling
School of
in Computer
Java Science and Engineering
May 14,
VIT-AP
2022 University
47 / 47