CHAPTER-5 Exception Handling
CHAPTER-5 Exception Handling
CHAPTER - 5
Exception Handling
2023
Content of the lecture
Exception
Types Of Exception
Exception Handling
Hierarchy of Exception Handling
Try-Catch-Final Blocks,
User Defined Exceptions
Exception Handling
The process of converting system error messages into user friendly error
message is known as Exception handling
Predefined exception
Are defined by sun microsystem and integrated in jdk to deal with universal
problems
Predefined exception divided in two. they are
Types of Exception Handling
Asynchronous exception :This Exception deal with hardware related problem. There is pre
Synchronous Exception : Deal with programmatic error. To deal with those error ja-
Checked exception
Unchecked exception
Types of Exception Handling
Checked exception is one which deal with compile time error. such as
class not found exception and Interface not found exception.
Unchecked exception is one which deal with programmatic run time er-
ror. such are ArithemeticException , NumberFormatException , ArrayIn-
dexOutOfBoundException and etc
Hierarchy of Exception classes
Handling the Exception
Example:
java.lang.ArrayIndexOutOfBoundsException: 2
at ExceptionExample.main(ExceptionExample.java:4)
Inside try block we write the block of statements which causes exceptions at
run time in other words try block always contains problematic statements.
Important points about try block
If any exception occurs in try block then CPU controls comes out of the try block and exe-
cutes appropriate catch block.
After executing appropriate catch block, even through we use run time statement, CPU con-
trol never goes to try block to execute the rest of the statements.
Each and every try block must be immediately followed by catch block that is no intermedi-
ate statements are allowed between try and catch block.
Syntax
try
{
.....
}
/* Here no other statements are allowed
between try and catch block */
catch()
{………..}
Each and every try block must contains at least one catch block. But it is highly
recommended to write multiple catch blocks for generating multiple user
friendly error messages.
One try block can contains another try block that is nested or inner try block
can be possible.
catch block
Inside catch block we write the block of statements which will generates user
friendly error messages.
catch block important points
Catch block will be executed when exception occurs in try block.
You can write multiple catch blocks for generating multiple user friendly error messages
to make your application strong. You can see below example.
At a time only one catch block will execute out of multiple catch blocks.
finally Block in Exception Handling
Inside finally block we write the block of statements which will relinquish
(released or close or terminate) the resource (file or database) where data
store permanently.
finally block important points
Finally block will execute compulsory
Writing finally block is optional.
You can write finally block for the entire java program
In some of the circumstances one can also write try and catch block
in finally block.
Example
Catching Multiple Exceptions
Handle multiple possible exceptions by multiple successive catch blocks
try {
// code that might throw multiple exception
}
catch (IOException e) {
// handle IOException and all subclasses
}
catch (ClassNotFoundException e2) {
// handle ClassNotFoundException
}
Rule: At a time only one Exception is occurred and at a time only one catch block is executed.
Rule: All catch blocks must be ordered from most specific to most general
i.e. catch for ArithmeticException must come before catch for Exception .
Example
class TestMultipleCatchBlock1{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e){System.out.println("common task completed");}
catch(ArithmeticException e)
{System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e)
{System.out.println("task 2 completed");}
System.out.println("rest of the code...");
}
}
Output:
Java Nested try block
Java Nested try block: The try block within a try block is known as nested try block in java.
Why use nested try block: Sometimes a situation may arise where a part of a block may cause on
error and the entire block itself may cause another error. In such cases, exception handlers have to b
nested. ....
Syntax: try
{ statement 1;
statement 2;
try
{ statement 1;
statement 2;
} catch(Exception e) {
} }
catch(Exception e)
{ }
....
Java nested try example
class Excep6{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b=39/0;
}catch (ArithmeticException e){System.out.println("Division by zero");}
try{
int a[]=new int[5];
a[5]=4;
Java nested try example
System.out.println("other statement");
}catch(Exception e){System.out.println("handeled");}
System.out.println("normal flow..");
}
Example : Multi catch block
Throw is a keyword in java language which is used to throw any user defined
exception to the same signature of method in which the exception is raised.
Throw keyword always should exist within method body.
whenever method body contain throw keyword then the caller method
should be followed by throws keyword.
Syntax:
throw new exception_class("error message");
Example
rived class constructor.
Custom or User Defined Exception in Java
Save the program with public class name.java
Example
Difference between throw and throws in Java
Adama Science and Technology University
School of Electrical Engineering And Computing 2023 G.C