exception handler

An exception handler is code that stipulates what a program will do when an anomalous event disrupts the normal flow of that program’s instructions. An exception, in a computer context, is an unplanned event that occurs while a program is executing and disrupts the flow of its instructions.

In Java, there are two types of exceptions: Checked exceptions are those that are checked when the code is compiled; for the most part, the program should be able to recover from these, and exception handlers are written into programs to define what it should do according to specified conditions. Unchecked exceptions are less foreseen events that occur when a program runs. In some cases, these can be handled but usually they cannot. Errors are one type of unchecked exception.

Programs communicate with the operating system and other software systems through various layers. When a checked exception occurs, the method where it happened creates an exception object that contains information like what type of exception it was and the state of the program when it happened. The creation and subsequent passing of this object is called throwing an exception. The method passes the object to the runtime system, which searches on through the next layers seeking exception handler code that matches what is specified in the exception object. In that case, the exception handler is said to catch the exception. If an appropriate exception handler is not found, the operating system generates a fatal exception message, which means that the program must close and perhaps the system must shut down as well.

Here’s a pseudocode example of an exception handler written for a previously declared exception type, EmptyLineException:

try {

    line = console.readLine();

    if (line.length() == 0) {

        throw new EmptyLineException("The line read from console was empty!");


    console.printLine("Hello %s!" % line);

    console.printLine("The program ran successfully");


catch (EmptyLineException e) {



catch (Exception e) {

    console.printLine("Error: " + e.message());


finally {

    console.printLine("The program terminates now");



See also: error handling

This was last updated in July 2016

Continue Reading About exception handler

Dig Deeper on Software development best practices and processes

App Architecture
Software Quality
Cloud Computing