You are on page 1of 13

Faculty of Computing and Information Technology (FCIT)

Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Lab No. 10: Exception Handling


Objective:
Introduction to Exception Handling. The Exception Hierarchy in java.

Scope:
An Exception can be anything which interrupts the normal flow of the program. When an excep-
tion occurs program processing gets terminated and doesn’t continue further. In such cases we
get a system generated error message. The good thing about exceptions is that they can be han-
dled. Exceptions are conditions within the code. A developer can handle such conditions and
take necessary corrective actions.

Exception Hierarchy:
All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of
the Throwable class. Other than the exception class there is another subclass called Error which is de-
rived from the Throwable class.

Errors are not normally trapped form the Java programs. These conditions normally happen in case of
severe failures, which are not handled by the java programs. Errors are generated to indicate errors gen-
erated by the runtime environment. Example : JVM is out of Memory. Normally programs cannot re-

cover from errors. The Exception class has two main subclasses: IOException class and RuntimeExcep-
Object Oriented Programming
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

tion Class.

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 1: The following programs handle an exception:

Example 2: Implementation of multiple Catch blocks:

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 3: Implementation of Nested try catch:

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 1: Implementation throwing an exception

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 2: Implementation throwing an IOException with normal flow.

Example 3: Implementation throwing a runtime Exception.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 4: To demonstrate using our user-defined exception, the following CheckingAccount class con-
tains a withdraw() method that throws an InsufficientFundsException.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 5: The following BankDemo program demonstrates invoking the deposit() and withdraw()
methods of CheckingAccount.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 6: Java finally example where exception doesn't occur.

Example 7: Java finally example where exception occurs and not handled.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 8: Java finally example where exception occurs and handled.

Exercise:

1. Write a program that prompts the user to read two integers and displays their sum. Your program
should prompt the user to read the number again if the input is incorrect.

Object Oriented Programming using Lab Manual


JAVA

You might also like