Science  People  Locations  Timeline
Index: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Home > Exception handling


 

Exception handling is a programming language mechanism designed to handle runtime errors or other problems (exceptions) inside a computer program.

1 Goals of exceptions

Exception handling is intended to facilitate use of reasonable mechanisms for handling erroneous or exceptional situations that arise in programs. Exception handling can be used to pass information about error situations that occur within library code to its users, and selectively respond to those errors.

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.

2 Exception safety

A piece of code is said to be exception-safe if raising any exceptions from operations within the code will not produce ill-effects, such as memory leaks, garbled data or invalid output. Exception-safe code must satisfy invariants placed on the code even if exceptions occur. There are several levels of exception safety:

Usually at least basic exception safety is required. Failure transparency is difficult to implement, and is usually not possible in libraries where complete knowledge of the application is not available.

3 Exception support in programming languages

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 int main() { try { // do something (might throw an exception) } catch (std::exception& e) { // handle exception e } catch (...) { // unknown exception, should not happen } }

In C++, a resource acquisition is initialization technique can be used to clean up resources in exceptional situations.

An example of exception handling in Java:

try { // Normal execution path } catch (ExampleException ee) { // Control jumps here if ExampleException or any of its subclasses happen } catch (Throwable t) { // Throwable is the superinterface of all exception classes, so this is equivalent to C++'s catch (...) } finally { // This optional section is executed upon termination of any of the try or catch blocks above }

Some operating systemIn computing, an operating system OS is the system software responsible for the direct control and management of hardware and basic system operations, as well as running application software such as word processing programs and web browsers. In general, ts also have similar features, for example Microsoft WindowsImage use policy. Microsoft Windows is a range of commercial operating environments for personal computers. The range was first introduced by Microsoft in 1985 and eventually has come to dominate the world personal computer market. All recent versions of' "structured exception handling".

See Also: Triple faultA triple fault is a special kind of exception generated by the CPU when an exception occurs while the CPU is trying to invoke the double fault exception handler, which itself handles exceptions occurring while trying to invoke a regular exception handler.

Programming

Read more »

Non User