Error Handling


Software Development; Coding Techniques; Software Engineering


Error handling is the process of responding appropriately to errors that occur during program execution. Errors may be addressed using unstructured or structured programming techniques. Errors can be handled by automatically running alternative code to accomplish the same task or by informing the user or developer of the error, ideally allowing them to correct the problem causing the error.



Computer programmers and users are human, and mistakes may be introduced either in a program's code itself or through incorrect input or system requirements at run time. Errors occur when a computer program fails to compile, becomes unresponsive, fails to operate as intended, or terminates unexpectedly. Programs should be designed and written so that errors are minimized, but they must also deal effectively with the errors that do occur. Certain types of errors called exceptions, which occur during execution, may be anticipated in such a way that the program can respond appropriately. The process of finding and correcting errors is called debugging. Different techniques are employed to handle different types of errors when they are found.

In the Java language, if an object belongs to the error superclass “throwable,” it will either be an error or an exception. Error subclasses include Linkage Error, Assertion Error, Thread Death, Virtual Machine Error, IO Error, and

In the Java language, if an object belongs to the error superclass “throwable,” it will either be an error or an exception. Error subclasses include Linkage Error, Assertion Error, Thread Death, Virtual Machine Error, IO Error, and others. Subclasses of Virtual Machine Er ror include Out of Memory Er ror, Stack Overflow Er ror, and others.

Syntax errors are errors that occur due to violations of a programming language's syntax, or rules governing how elements such as words and symbols are arranged. For example, if a language specifies that all strings must be enclosed in double quotes, the following line of code would generate a syntax error due to the use of single quotes:

Error Handling

Syntax errors prevent a program from compiling. Compiling is the process of converting computer code into a form that can be executed and used.

Logic errors occur when code is syntactically correct, so the program compiles, but the resulting program does not function as intended. For example, if a program is designed to display the names of all employees hired during the month of January but it displays the names of employees hired in February instead, a logic error has occurred. Proofreading, testing, and feedback are all important steps in finding and handling syntax and logic errors. Programmers also use software debugging tools to find and correct these errors.

Run-time errors are errors that occur when a program is in use. They are typically caused by the program encountering issues outside of its source code, such as invalid parameters in the larger computer system or incorrect data input. Run-time errors are not detected when a program is compiled, and they may cause a program to become unresponsive or terminate unexpectedly. Typical run-time errors include divide-by-zero errors and attempting to access a required file that has been deleted. The negative effects of run-time errors can be minimized through the use of error handlers, or sections of code designed to correct errors. Correction may involve running alternative code to accomplish the requested task. The program may also handle errors by informing the user and allowing them to resolve the problem.


Errors may be addressed through either structured or unstructured error handling. Unstructured error handling works by directing the program to a specific line of code or by instructing the program to proceed to the next line of code if an error occurs. This is a form of unstructured programming. Unstructured programs are difficult to debug, modify, and maintain. For this reason, programmers often use structured error handling instead.

Structured error handlers in many programming languages use a construct called a try/catch block. A try/catch block is structured as follows:

Code that may cause run-time errors
Code that runs if a run-time error has occurred

Try/catch blocks may contain multiple catch blocks that catch different types of errors.

While try/catch is an important technique, other methodologies can also be used to handle errors. Functions can use return codes to allow error conditions to be detected and dealt with. Guard clauses and data validation can be used to prevent errors from occurring by correcting error conditions automatically or by offering users the chance to correct errors before they occur.


A retailer may use a computer program to calculate the average price of all items sold in one day. The program would prompt the user to input the number of items sold and the total dollar amount of sales, and then it would divide the dollar amount by the number of items. If the user enters 0 for the total number of items sold, a divide-by-zero error will occur. The following error handler could be used to handle this error:

Prompt the user for number of items sold
Prompt the user for total amount of sales in dollars
Calculate the average item price by dividing by the total amount of sales in dollars by the total number of items sold
Display the average item price
CATCH divide-by-zero error
Inform the user that they must enter a nonzero value for the number of items sold
CATCH any other error
Code that executes for any error other than divide by zero

The try block includes the code that may cause the error, and the first catch block contains the code that will run if a divide-by-zero error occurs. The second catch block is included because it is good practice to add code to handle any potential errors that do not have a dedicated error handler.


For a computer program to accomplish the purpose for which it was designed, errors must be detected, minimized, and handled appropriately. Computer programs that do not handle errors well suffer from unexpected failures and are frustrating to use. Programmers must anticipate the types of errors that may occur in their programs and develop code with error handling in mind. Failure to do so will limit the stability and usefulness of the software they create. As long as errors are possible, error handling will remain a critical part of computer programming.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2015.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Schneider, David I. An Introduction to Programming Using Visual Basic. 10th ed., Pearson, 2017.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Elsevier, 2016.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.