Exception handling



         


runtime errors or other problems (exceptions) inside a computer program.

A possible role of exception handling is to allow the program to continue its normal operation and prevent crashing and displaying of cryptic error messages to the user. In many cases, it is sufficient to stop the program and produce an error report; the difference with systems that do not use exceptions to signal improper program executions is that with proper exception handling, the erroneous condition may be pointed precisely, whereas otherwise it is often detected later, making debugging difficult.

Certain computer languages such as Ada, C++, Objective-C, Java, Eiffel and Ocaml have built-in support for exceptions and exception handling. In those languages, the advent of an exception (more precisely, an exception handled by the language) unwinds the stack of function calls until an exception handler is found. That is, if function f has a handler H for exception E, calls function g, which in turn calls function h, and an exception occurs in f, then functions h and g will be terminated and H will handle E.

An example of exception handling in C++:

#include <exception> // ... int main() { try { // do something (might throw an exception) } catch (std::exception& e) { // handle exception e } catch (...) { // unknown exception, should not happen } // ... }

Some operating systems also have similar features, for example Microsoft Windows "structured exception handling".

See Also: Triple fault





  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License