Home » Online Computer Education » Learn Fundamentals of Java Programming “Exception Handling in Java Programming ” Lesson 26

Learn Fundamentals of Java Programming “Exception Handling in Java Programming ” Lesson 26

Exception Handling

By now you may have written quite a few programs. Some of your programs when executed may have terminated unexpectedly with runtime errors. The errors could have occurred because an array index reference was out of range, an attempt was made to divide an integer by zero, there was insufficient computer memory and so on. Such an error situation that is unexpected in the program execution and causes it to terminate unexpectedly is called an exception. As a programmer, you should anticipate exceptions in your program that could lead to unpredictable results and handle the exceptions. Consider the program in Figure 1.10a that has a method called divide(). The method takes two numbers as parameters, divides them and returns the quotient:

int divide(dividend, divi sor) {
return dividend/divisor;

Since division by 0 in integer arithmetic causes a program to terminate prematurely, a call to the function divide such as divide(10,0) ; will cause the program to fail (Figure 1.10j).

Java fundamentals  array  20

Note: If the method divide() had used floating point numbers instead of integers, there would not have been an error, since in floating-point arithmetic, division by zero is allowed — it results in infinity, which would be displayed as Infinity.
Java provides an exception handling mechanism so that a program is able to deal

with exceptions, and continue executing or terminate gracefully. The basic idea in exception handling is to

 Denote an exception block – Identify areas in the code where errors can occur
 Catch the exception – Receive the error information
 Handle the exception – Take corrective action to recover from the error

Java provides the following keywords to handle an exception:

1. try – A try block surrounds the part of the code that can generate exception(s).
2. catch – The catch blocks follow a try block. A catch block contains the exception handler – specific code that is executed when the exception occurs. Multiple catch blocks following a try block can handle different types of exceptions.
The structure of a try-catch statement block for exception handling is as below:

try {
// Part of the program where an exception might occur
catch (exceptiontype1 argument1) {
// Handle exception of the exceptiontype1
catch (exceptiontype2 argument2) {
// Handle exception of the exceptiontype2
finally {
//Code to be executed when the try block exits

The try block is examined during execution to detect any exceptions that may be thrown by any statements or any calls to methods within the block. If an exception is thrown, an exception object is created and thrown. The program execution stops at that point and control enters the catch block whose argument matches the type of the exception object thrown. If a match is found the statements in that catch block are executed for handling the exception.
If no exception is thrown during execution of the statements in the try block, the catch clauses that follow the try block are not executed. Execution continues at the statement after the last catch clause.
The optional finally block is always executed when the try block exits. This means the finally block is executed whether an exception occurs or not. Programmers use this block to put in clean up code, for example, freeing up resources allocated in the try block.
The following code fragment handles a division by zero exception:

try {
int quotient = divide(10,0);
} catch (Exception e) {

We catch an exception of the type Exception. An object of the class Exception is a “catch all” exception that returns a general error message encountered during program execution. The exception handler in the catch block uses the getMessage() method of the Exception class to print the error that caused the exception.

Java fundamentals  array  19

Figures 1.9k and 1.9l show the Exception Handling Demo program and its output respectively.


The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *