FACTOID # 4: China's labor force stands at 706 million people, almost three times that of Europe and twice that of North and South America combined
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Error handling

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

Contents

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.


Exception handling makes the signal-handling techinque used in other languages obsolete. Consider this code in C:

 file = fopen("hi.txt", "rb"); if (file == NULL) { printf("hi.txt was not found. n"); } 

fopen is a call that is known to fail. The specified file might not be there, the user might not have read permission to it, etc. fopen() signals that something exceptional has occured by setting the variable file to NULL.


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:

  • Failure transparency, operations are guaranteed to succeed and satisfy all requirements even in presence of exceptional situations. (best)
  • commit or rollback semantics, operations are guaranteed to have no side effects, if exceptions occur.
  • basic exception safety, partial execution of operations due to exceptions can cause side effects, but invariants are preserved.
  • minimal exception safety, erroneous situations or invalid input will not cause a crash.
  • no exception safety, no guarantees are made. (worst)

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.


Exception support in programming languages

Certain computer languages such as Ada, C++, D, 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.


It is worth noting that Common Lisp and Dylan have a Condition System which encompasses the aforementioned exception handling systems. In those languages the advent of a condition (a "generalisation of an [exception]" according to Kent Pitman) implies a function call, and only late in the exception handler the decision to unwind the stack may be taken.


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 } } 

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 systems also have similar features, for example Microsoft Windows' "structured exception handling".


See also


  Results from FactBites:
 
An Example Error Handling Script (209 words)
Error scripts have extra environment variables passed to them, in addition to all of the CGI 1.1 variables.
Some error messages might require headers beyond which are in the CGI specification.
This should handle all errors in # almost the same fashion as NCSA HTTPd 1.4 would internally.
VB Helper Tutorial: Bug Proofing - Error Handling Fundamentals (2894 words)
A developer who later adds a new error handler to the end of the routine may not notice that the code drops through the end of the routine.
If no error handler is in effect, Visual Basic moves up the call stack to the calling routine to see if an error handler is currently installed there.
If no error handler is installed in the calling routine either, Visual Basic continues moving up the call stack until it finds a routine with an error handler installed.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.