Important: The information in this document is obsolete and should not be used for new development.
About the Exception Manager
An exception is an error or other special condition detected by the microprocessor in the course of program execution. When an exception occurs, the Operating System transfers control synchronously to the relevant exception handler, which attempts to recover gracefully from the error or special condition. The kinds of errors or other conditions that give rise to exceptions differ from one processor to another. On 680x0 processors, for example, an exception is generated if the currently executing program attempts to divide by zero. By contrast, the PowerPC processor does not generate an exception under
that condition.In general, applications or other types of software (including much of the Macintosh Operating System and the Macintosh Toolbox) cannot tolerate the occurrence of exceptions. To provide some measure of protection from potentially fatal exceptions, the Operating System installs its own set of exception handlers. You can, if necessary, use the Exception Manager to install application-specific exception handlers. Any exception handlers that you install apply only to your current context and only to exceptions that are not first intercepted and handled by the Operating System.
When your exception handler is called, the Exception Manager passes it a parameter that contains information about the state of the machine at the time the exception occurred. On PowerPC processor-based Macintosh computers, this information includes
- IMPORTANT
- Not all exceptions that occur in your application's context are passed to your exception handler. Certain exceptions (for example, page faults) are handled completely by the Operating System's exception handlers. As a result, those exceptions do not affect the normal execution of your application or other software.
Your exception handler can handle the exception in various ways. For example, it might modify the machine state and then resume execution. Similarly, your exception handler might simply transfer control to some other code. In rare instances, however, your exception handler might not be able to handle the exception; when this happens, the exception is usually fatal to your application.
- the kind of exception that occurred
- the contents of the 32 general-purpose registers
- the contents of the special-purpose registers (such as the Link Register and the Condition Register)
- the contents of the 32 floating-point registers
Exception Contexts
In the first version of the system software for PowerPC processor-based Macintosh computers, each application can install its own exception handler, which remains the active handler as long as that application is the current application. In other words, the exception handler of the current application is called for all exceptions not intercepted
by the Operating System. In general, this mechanism results in the execution of the appropriate exception handler. It's possible, however, for code you install to cause exceptions that are handled by some other application's exception handler. For instance, exceptions that arise during the asynchronous execution of code (such as VBL tasks, Time Manager tasks, and I/O completion routines) are handled by the exception handler of whatever application happens to be the current application at the time the exception occurs. If that application has not installed an exception handler, the exception might not be handled.All asynchronous code executed in the first version of the system software for PowerPC processor-based Macintosh computers is executed under the 68LC040 Emulator, in which case the exceptions are handled using the existing 680x0 mechanisms. If, however, a routine executed asynchronously calls some code that is native PowerPC code, and if that native code causes an exception to occur, then the current application's exception handler (if any) is called to handle the exception.
Types of Exceptions
In the first version of the system software for PowerPC processor-based Macintosh computers, the following conditions can cause exceptions while your application or other software is executing in native mode:
The Exception Manager defines a number of exception codes that indicate these and other conditions. An exception code is a constant that indicates which kind of exception has occurred.
- an attempt to write to write-protected memory
- an attempt to access (that is, read, write, or fetch) data at a logical address that is
not assigned- an attempt to execute trap instructions or other instructions that are not part of the supported application programming interface
- an attempt to execute invalid instructions or an invalid form of a valid instruction
- an attempt to execute privileged instructions when the system is not in
privileged mode- in appropriate circumstances, reaching a breakpoint
- in appropriate circumstances, reaching a trace point
typedef unsigned long ExceptionKind; /*kind of exception*/ enum { /*exception codes*/ unknownException = 0, /*unknown exception type*/ illegalInstructionException = 1, /*illegal instruction*/ trapException = 2, /*unknown trap type*/ accessException = 3, /*failed memory access*/ unmappedMemoryException = 4, /*memory is unmapped*/ excludedMemoryException = 5, /*memory is excluded*/ readOnlyMemoryException = 6, /*memory is read-only*/ unresolvablePageFaultException = 7, /*unresolvable page fault*/ privilegeViolationException = 8, /*privilege violation*/ traceException = 9, /*trace*/ instructionBreakpointException = 10, /*instruction breakpoint*/ dataBreakpointException = 11, /*data breakpoint*/ integerException = 12, /*unused*/ floatingPointException = 13, /*floating point*/ stackOverflowException = 14, /*stack overflow*/ terminationException = 15 /*task is being terminated*/ };Not all of these exception codes are used in the first version of the system software for PowerPC processor-based Macintosh computers; see "Exception Kinds" on page 4-9 for a complete explanation of these constants.