Joined: 29 Nov 2006
|Posted: Tue Sep 07, 2004 9:45 pm Post subject: Trapping exceptions
|Exceptions are events generated outside the normal flow of control through a program or thread of execution. Such an event may arise due to a hardware event (such as a page fault) or through a software trap such as an attempt to access another processes memory space. The default action of the process is to terminate the process and produce diagnostic information.
TRAP_EXCEPTION@ and SET_TRAP@ provide the programmer with a method to trap these exception events and to act appropriately. This means that it is possible to trap (say) an underflow event and reset a variable to a known value (say zero).
This is achieved by maintaining a table of functions to be executed in the event of an exception. Only one exception handler may be installed for any particular exception event at any one time. So you may have two different handlers installed for two different exception events, but you may not have two handlers chained together for the same exception event. This also applies to mixed language programming where nominally different handlers are required for Fortran and C code. If you want to handle an exception differently in different parts of the code, you can remove one exception handler and install another.
Each exception event is identified by an exception event code. This is an integer value that is used to uniquely identify each of the possible exceptions that are trappable by the user. These codes are defined in the insert file exceptn.ins that is provided as part of the compiler system.
When an exception event occurs, the operating system copies the machine state into an area of memory. The image of the machine may be manipulated to correct the fault in order to resume execution in an orderly manner. Once the machine state has been saved, the exception handler searches for a handler offering the event to the following processes:
* Debugger first chance.
* The frame based handler installed by the program.
* Debugger second chance.
The frame based handler is the one installed by any main program compiled with the compiler. This handler is really a filter. It examines the exception event that has occurred and looks to see if the user program has installed a handler for that event. If such a handler routine is installed, control is passed back to the routine. If no handler is found, the Fortran program takes the default action or it terminates and the exception details are displayed for debugging purposes.
The two functions TRAP_EXCEPTION@ and SET_TRAP@ are used to install an event handler for a given event. SET_TRAP@ is used to trap the use of CTRL+C to break into program execution. More information on these functions is available in the FTN95 Users Guide.