Trapping Win32 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 trapable by the user. These codes are defined in the insert file except.ins which is provided as part of the FTN95 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 FTN95. 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.

Here is a summary of the FTN95 error and exception handling routines that are peculiar to Win32.

ACCESS_DETAILS@

Gets details of the access violation.

CLEAR_FLT_UNDERFLOW@

Clears a floating point underflow exception.

EXCEPTION_ADDRESS@

Finds the address of the instruction that generated the exception.

GET_VIRTUAL_COMMON_INFO@

Gets virtual common block details.

PRERR@

Prints the error message associated with a given error code.

RESTORE_DEFAULT_HANDLER@

Removes a user defined exception handler.

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited