0% found this document useful (0 votes)
19 views33 pages

Chapter 5

Uploaded by

biqilaaaddunyaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views33 pages

Chapter 5

Uploaded by

biqilaaaddunyaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Object Oriented Programming

CHAPTER - 5

Exception Handling
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

 Basically there are two types of error

• compile time error : It occurred due to poor understanding


of language
• Run time error : Occurred if user inputs invalid data
Types of Exception Handling

 In java there are two types of exception

1. Pre defined Exception

2. User defined exception

Predefined exception

 Are defined by sun microsystem and integrated in jdk to deal


with universal problems
Types of Exception Handling

 Predefined exception divided in two. they are

 Asynchronous exception :This Exception deal with hardware related problem.

There is pre defined class Java.lang.Error that used to deal with those problems.

 Synchronous Exception : Deal with programmatic error. To deal with those

error java.lang.Exception is given in jdk.

 Synchronous exception categorized into two

 Checked exception

 Unchecked exception
Types of Exception Handling

 Checked exception is one which deal with compile time


error regarding class not found exception and Interface
not found exception.
 Unchecked exception is one which deal with
programmatic run time error such are
ArithemeticException , NumberFormatException ,
ArrayIndexOutOfBoundException and etc
How exception occur in java run time
environment
Hierarchy of Exception classes
Handling the Exception

 Use Five keywords for Handling the Exception


 try
 catch
 finally
 throws
 throw
Syntax
try
{
// statements causes problem at run time
}
catch(type of exception-1 object-1)
{
// statements provides user friendly error message
}
catch(type of exception-2 object-2)
{
// statements provides user friendly error message
}
finally
{
// statements which will execute compulsory
}
Exception Message Details
Exception message format:
[exception class]: [additional description of
exception]
at [class].[method]([file]:[line number])

Example:
java.lang.ArrayIndexOutOfBoundsException: 2
at
ExceptionExample.main(ExceptionExample.java:4)
 What exception class? ArrayIndexOutOfBoundsException
 Which array index is out of bounds? 2
 What method throws the exception? main

 What file contains the method? ExceptionExample.java

 What line of the file throws the 4

exception?
Example without Exception Handling
Example with Exception Handling

Output:
Denominator can’t be Zero
try and catch block

 Inside try block we write the block of statements which causes


executions 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 to the
try block and executes appropriate catch block.
 After executing appropriate catch block, even through we use run time
statement, CPU control never goes to try block to execute the rest of the
statements.
 Each and every try block must be immediately followed by 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 execute 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.
 in catch block you declare an object of sub class and it will be internally
referenced by JVM.
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

Output
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.
Example

Output
Compile Time Error
Example : Multi catch block
 If you have to perform different
tasks at the occurrence of
different Exceptions, use java
multi catch block.

Output
task1 completed
rest of the code...
Java Nested try block
Syntax:
 Java Nested try block: The try
block within a try block is known ....
try
as nested try block in java.
{ statement 1;
 Why use nested try block: statement 2;
try
Sometimes a situation may arise
{ statement 1;
where a part of a block may statement 2;
} catch(Exception e) {
cause one error and the entire
} }
block itself may cause another catch(Exception e)
{ }
error. In such cases, exception
....
handlers have to be nested.
Java nested try example
Throws

 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.
 Syntax
Example
Throw

 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 than the
caller method should be followed by throws keyword.
 Syntax:
Example
Custom Exception in Java
 It is exception designed by programmer.
 Rules to design user defined Exception
 Create a package with valid user defined name.
 Create any user defined class.
 Make that user defined class as derived class of
Exception or RuntimeException class.
 Declare parametrized constructor with string variable.
 call super class constructor by passing string variable
within the derived class constructor.
 Save the program with public class name.java
Example
Example
Example
Difference between throw and throws in Java

You might also like