Exception

An exception (short for "exceptional event") is an error or unexpected event that happens while a program is running. When an exception occurs, it interrupts the flow of the program. If the program can handle and process the exception, it may continue running. If an exception is not handled, the program may be forced to quit.

Multiple programming languages support exceptions, though they are used in different ways. For example, exceptions are an integral part of the Java language and are often to control the flow of a program. Java includes an Exception class, which has dozens of subclasses, such as TimeoutException, UserException, and IOException. Subclasses like IOException contain more specific exceptions like FileNotFoundException and CharacterCodingException that can be "thrown" if a file is not found or the character encoding of a string is not recognized.

Other languages only use exceptions to catch fundamental runtime errors, such as failure allocating memory or system-level errors. For example, a C++ program may throw the bad_alloc exception when memory cannot be allocated and the system_error exception when the operating system produces an error.

Exception Handling

A well-written computer program checks for exceptions and handles them appropriately. This means the developer must check for likely exceptions and write code to process them. If a program handles exceptions well, unexpected errors can be detected and managed without crashing the program.

Exceptions are "thrown" when then occur and are "caught" by some other code in the program. They can be thrown explicitly using the throw statement or implicitly within a try clause. Below is an example of "try / catch" syntax in Java. The following code attempts to divide by zero, but throws an ArithmeticException exception and returns 0 as the result.

 1. int a = 11;
 2. int b = 0;
 3. int result = 0;
 4. try {
 5.   int c = a / b;
 6.   result = c;
 7. } catch(ArithmeticException ex) {
 8.   result = 0;
 9. }
10. return result;

An exception is thrown on line 5 (when 11 is divided by 0), so the remainder of the try statement (line 6) is not executed. Instead, the exception is caught on line 7 and a result of 0 is returned.

Updated May 6, 2017 by Per C.

quizTest Your Knowledge

Which of the following is not a variable that can be set when scheduling a cron job?

A
Hour
0%
B
Minute
0%
C
Second
0%
D
Month
0%
Correct! Incorrect!     View the Cron definition.
More Quizzes →

The Tech Terms Computer Dictionary

The definition of Exception on this page is an original definition written by the TechTerms.com team. If you would like to reference this page or cite this definition, please use the green citation links above.

The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please contact us.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.