Definition

exception handler

Contributor(s): Matthew Haughn

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) {

    console.printLine("Hello!");

}

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

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

How often new coders forget these. They are a great way for providing info on why the application step failed. It's also not just for Java but VB as well. Just a different method and format.
Cancel

-ADS BY GOOGLE

File Extensions and File Formats

SearchCloudApplications

SearchSoftwareQuality

SearchFinancialApplications

SearchSAP

SearchManufacturingERP

Close