Difference between revisions of "Condition handler"

From Computer History Wiki
Jump to: navigation, search
(A pretty good start)
 
(Reorganize a bit, say more about utility)
Line 3: Line 3:
 
When such an event (be it caused by some sort of hardware-detected issue, e.g. 'divide by zero', or purely software, e.g. 'unexpected end-of-file') happens, program execution is diverted to an condition handler for that specific condition, which is expected to deal with it.
 
When such an event (be it caused by some sort of hardware-detected issue, e.g. 'divide by zero', or purely software, e.g. 'unexpected end-of-file') happens, program execution is diverted to an condition handler for that specific condition, which is expected to deal with it.
  
In procedural languages (such as [[ALGOL]] and [[C (language)|C]] - although neither one normally supports condition handlers), each [[subroutine|procedure]] can set up one or more condition handlers on entry to an instance of the procedure. From then on, until that instance exits, any time that condition is '''signalled''' (in either that procedure, or any procedure it calls, and so on), execution jumps to that condition handler.
+
In procedural languages (such as [[ALGOL]] and [[C (language)|C]] - although neither one normally supports condition handlers), each [[subroutine|procedure]] can set up one or more condition handlers on entry to an instance of the procedure. From then on, until that instance exits, any time that condition is '''signalled''' (in either that procedure, or any procedure it calls, and so on), execution jumps to that condition handler. Should an inferior procedure set up a condition handler for that same condition, the inferior procedure's handler will be '''invoked''' preferentially.  
  
Should an inferior procedure set up a condition handler for that same condition, the inferior procedure's handler will be '''invoked''' preferentially. Information about active condition handlers is generally maintained in the [[call stack]], so that handlers are automatically discarded when a procedure instance that set them up exits. When looking for a handler for a signalled condition, the condition system starts at the bottom of the call stack, and works its way upwards, looking for an active handler for rhat condition.
+
Condition handlers are usually provided with a number of ways to terminate their execution:
  
Condition handlers are usually provided with a number of ways to terminate their execution; they may (after repairing the condition that led to the signalling of the condition) resume operation at the place where the condition was signalled; they may '''re-signal''' the condition to a higher-level handler for that condition; they may signal another condition; or they may terminate the program (if the problem cannot be repaired).
+
* they may (after repairing the condition that led to the signalling of the condition) '''resume''' operation at the place where the condition was signalled (although not all condition systems provide this option);
 +
* they may '''re-signal''' the condition to a higher-level handler for that same condition;
 +
* they may signal a different condition;
 +
* they may '''terminate''' the program (if the problem cannot be repaired).
  
==Special conditions==
+
Some languages use the term 'throw' for raising an exception, and 'catch' for the handler.
  
In addition to the above, there are a variety of special operations.
+
==Implementation approaches==
 +
 
 +
There are a number of approaches to implementing condition handlers.
 +
 
 +
One is to keep information about active condition handlers for any invocation in its [[stack frame]] in the [[call stack]], so that handlers are automatically discarded when a procedure instance that set them up exits. When looking for a handler for a signalled condition, the condition system starts at the bottom of the call stack, and works its way upwards, looking for an active handler for rhat condition. This approach has the drawback that it is hard to provide resume semantics.
 +
 
 +
Another possibility is to have a special stack on which condition handlers are listed, along with pointers to routines given the task of handling that condition. These must be un-stacked when the procedure invocation which set them up exits, which is extra overhead, but is more flexible.
 +
 
 +
==Special operations==
 +
 
 +
In addition to the above, there are a variety of special operations associated with condition systems.
  
 
If no condition handler for a particular signalled condition is found in the call stack, the condition system may respond by signalling an 'unhandled condition' condition. Usually a handler for this condition is provided in the top-level procedure, to catch otherwise un-handled conditions.
 
If no condition handler for a particular signalled condition is found in the call stack, the condition system may respond by signalling an 'unhandled condition' condition. Usually a handler for this condition is provided in the top-level procedure, to catch otherwise un-handled conditions.
  
An '''unwind protect''' is a special condition handler which, if set up in any procedure on the call stack, if the call stack has to be [[unwind|unwound]] to get to a handler for a particular signalled condition, for any procedure invocation which is unwound which has a handler for 'unwind protect', that handler is called 'on the way through'. This allows that instance to do any cleanup it needs to, before it is terminated by the condition system.
+
An '''unwind protect''' is a special condition handler which, if set up in any procedure instance on the call stack, if the call stack has to be [[unwind|unwound]] ''through'' that instance to get to a handler for a particular signalled condition, for any procedure invocation which is unwound which has a handler for 'unwind protect', that handler is called 'on the way through'. This allows that instance to do any cleanup it needs to, before it is terminated by the condition system.
 +
 
 +
==Utility==
 +
 
 +
Condition handlers are usually more use in very large programs; in small programs it is easy to simply return special values, which indicate an error, through a small number of level of procedure call. In large systems, where it may be desired to handle a condition at a much higher level than where it happens, this would require a lot of special-case handling in many intervening procedures. Signalling a condition, on the other hand, can tranfer control directly to the handler, without needing extra code in all the intervening procedures.
 +
 
 +
An example is a [[compiler]] using a [[recursive descent]] [[parser]], which in turn calls a [[lexical analyzer]]. If the latter discovers an un-expected 'end of file', there can be an extremely large number of procedure invocations in between where that is discovered, and where it is desirable to handle it.

Revision as of 02:09, 23 October 2017

A condition handler (sometimes exception handler) refers to a mechanism in some programming languages which allows the programmer to provide code to handle the occurrence of a condition.

When such an event (be it caused by some sort of hardware-detected issue, e.g. 'divide by zero', or purely software, e.g. 'unexpected end-of-file') happens, program execution is diverted to an condition handler for that specific condition, which is expected to deal with it.

In procedural languages (such as ALGOL and C - although neither one normally supports condition handlers), each procedure can set up one or more condition handlers on entry to an instance of the procedure. From then on, until that instance exits, any time that condition is signalled (in either that procedure, or any procedure it calls, and so on), execution jumps to that condition handler. Should an inferior procedure set up a condition handler for that same condition, the inferior procedure's handler will be invoked preferentially.

Condition handlers are usually provided with a number of ways to terminate their execution:

  • they may (after repairing the condition that led to the signalling of the condition) resume operation at the place where the condition was signalled (although not all condition systems provide this option);
  • they may re-signal the condition to a higher-level handler for that same condition;
  • they may signal a different condition;
  • they may terminate the program (if the problem cannot be repaired).

Some languages use the term 'throw' for raising an exception, and 'catch' for the handler.

Implementation approaches

There are a number of approaches to implementing condition handlers.

One is to keep information about active condition handlers for any invocation in its stack frame in the call stack, so that handlers are automatically discarded when a procedure instance that set them up exits. When looking for a handler for a signalled condition, the condition system starts at the bottom of the call stack, and works its way upwards, looking for an active handler for rhat condition. This approach has the drawback that it is hard to provide resume semantics.

Another possibility is to have a special stack on which condition handlers are listed, along with pointers to routines given the task of handling that condition. These must be un-stacked when the procedure invocation which set them up exits, which is extra overhead, but is more flexible.

Special operations

In addition to the above, there are a variety of special operations associated with condition systems.

If no condition handler for a particular signalled condition is found in the call stack, the condition system may respond by signalling an 'unhandled condition' condition. Usually a handler for this condition is provided in the top-level procedure, to catch otherwise un-handled conditions.

An unwind protect is a special condition handler which, if set up in any procedure instance on the call stack, if the call stack has to be unwound through that instance to get to a handler for a particular signalled condition, for any procedure invocation which is unwound which has a handler for 'unwind protect', that handler is called 'on the way through'. This allows that instance to do any cleanup it needs to, before it is terminated by the condition system.

Utility

Condition handlers are usually more use in very large programs; in small programs it is easy to simply return special values, which indicate an error, through a small number of level of procedure call. In large systems, where it may be desired to handle a condition at a much higher level than where it happens, this would require a lot of special-case handling in many intervening procedures. Signalling a condition, on the other hand, can tranfer control directly to the handler, without needing extra code in all the intervening procedures.

An example is a compiler using a recursive descent parser, which in turn calls a lexical analyzer. If the latter discovers an un-expected 'end of file', there can be an extremely large number of procedure invocations in between where that is discovered, and where it is desirable to handle it.