Object Oriented Programming: Unit III: Exception Handling and Multithreading

Exception Handling

Benefits, Hierarchy, Types, Keywords used, Example Java Programs

Exception in Java is an indication of some unusual event. Usually it indicates the error. Let us first understand the concept. In Java, exception is handled, using five keywords try, catch, throw, throws and finally.

Unit 3 : Exception Handling and Multithreading

Exception Handling Basics

• Exception in Java is an indication of some unusual event. Usually it indicates the error. Let us first understand the concept. In Java, exception is handled, using five keywords try, catch, throw, throws and finally.

• The Java code that you may think may produce exception is placed within the try block. Let . us see one simple program in which the use of try and catch is done in order to handle the exception divide by zero.

Java Program [Exception Demo.Java]

class ExceptionDemo

{

public static void main(String args[])

{

try

{

int a,b;

a=5;

b=a/0;

}

catch(ArithmeticException e)

{

System.out.println("Divide by Zero\n");

}

System.out.println("...Executed catch statement");

}

}

Output

Divide by Zero

...Executed catch statement

Inside a try block as soon as the statement: b=a/0 gets executed then an arithmetic exception must be raised, this exception is caught by a catch block. Thus there must be a try-catch pair and catch block should be immediate follower of try statement. After execution of catch block the control must come on the next line. These are basically the exceptions thrown by java runtime systems.

Benefits of Exception Handling

Following are the benefits of exception handling -

1. Using exception the main application logic can be separated out from the code which may cause some unusual conditions.

2. When calling method encounters some error then the exception can be thrown. This avoids crashing of the entire application abruptly.

3. The working code and the error handling code can be separated out due to exception handling mechanism. Using exception handling, various types of errors in the source code can be grouped together.

4. Due to exception handling mechanism, the errors can be propagated up the method call stack i.e. problems occurring at the lower level can be handled by the higher up methods.

Exception Hierarchy

The exception hierarchy is derived from the base class Throwable. The Throwable class is further divided into two classes - Exceptions and Errors.

Exceptions: Exceptions are thrown if any kind of unusual condition occurs that can be caught. Sometimes it also happens that the exception could not be caught and the program may get terminated.

Errors: When any kind of serious problem occurs which could not be handled easily like OutOfMemoryError then an error is thrown.

Types of Exception

• There are two type of exceptions in Java

o Checked Exception: These types of exceptions need to be handled explicitly by the code itself either by using the try-catch block or by using throws. These exceptions are extended from the java.lang.Exception class.

For example: IOException which should be handled using the try-catch block.

o Unchecked Exception: These type of exceptions need not be handled explicitly. The Java Virtual Machine handles these type of exceptions. These exceptions are extended from java.lang.RuntimeException class.

For example:ArrayIndexOutOfBounds, NullPointerException, RunTimeException.

Keywords used in Exception Handling

• Various keywords used in handling the exception are -

try - A block of source code that is to be monitored for the exception.

catch - The catch block handles the specific type of exception along with the try block. Note that for each corresponding try block there exists the catch block.

finally - It specifies the code that must be executed even though exception may or may not Occur.

throw - This keyword is used to throw specific exception from the program code. throws - It specifies the exceptions that can be thrown by a particular method.

Review Question

1. What is exception? Explain exception handling in Java.

2. Explain the exception hierarchy.

3.Draw the exception hierarchy in java and explain with examples throwing and catching exceptions and the common exception.

4. Explain the different types of exceptions and the exception hierarchy in java with appropriate examples. 

Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : Benefits, Hierarchy, Types, Keywords used, Example Java Programs - Exception Handling