Exception.st
author claus
Mon, 22 Aug 1994 14:19:19 +0200
changeset 130 e09411db2573
parent 92 0c73b48551ac
child 142 c7844287bddf
permissions -rw-r--r--
emergencyhandler is now a one-arg-block

"
 COPYRIGHT (c) 1993 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Object subclass:#Exception
         instanceVariableNames:'signal parameter errorString
                                suspendedContext handlerContext
                                rejected
                                resumeBlock rejectBlock'
         classVariableNames:'EmergencyHandler'
         poolDictionaries:''
         category:'Kernel-Exceptions'
!

Exception comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
              All Rights Reserved

$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.11 1994-08-22 12:19:19 claus Exp $
'!

!Exception class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.11 1994-08-22 12:19:19 claus Exp $
"
!

documentation
"
    Instances of Exception are passed to a Signal handling block as argument.
    The handler block may perform various actions by sending corresponding messages
    to the exception handler. The following actions are possible:

        reject          - dont handle this signal;
                          another handler will be searched for, 
                          upper in the calling hierarchy

        proceed         - continue after the Signal>>raise, returning nil as value
        proceedWith:val - same, but return val from Signal>>raise

        return          - return from the Signal>>handle:do:, returning nil as value
        return:val      - same, but return val from Signal>>handle:do:

        restart         - restart the Signal>>handle:do:, after repairing

    Via the Exception object, the handler can also query the state of execution,
    where the Signal was raised, where the handler is, the signal which caused
    the error and the errorString passed when the signal was raised:

    instance variables:
        signal          - the signal which caused the exception
        parameter       - a parameter (if any) which was passed when raising
                          the signal (only if raised with #raiseWith:aParameter)

        errorString     - an errorString (only if raised wiith #raiseWith:errorString:)
        suspendedContext- the context where the signal raise occured
        handlerContext  - the context of the handler itself

        resumeBlock     - private to the exception; needed to perform resume action
        rejectBlock     - private to the exception; needed to perfomr reject action

    In case of an unhandled signal raise, Exceptions EmergenyHandler (a two-argument
    block) will be evaluated, getting the exception and context as arguments.
    The default emergeny handler will enter the debugger.
    For applications, which do not want Debuggers to come up, other handlers are
    possible.
    For example: to get the typical C++ behavior, use:
        Exception emergencyHandler:[:ex :con | Smalltalk exitWithCoreDump]


    instance variables:
        signal           <Signal>   the signal which is responsible for all of this
        parameter        <Object>   the parameter passed to raiseRequestWith: or nil (for raise)
        errorString      <String>   the signals notifierString, or the string given to raise
        suspendedContext <Context>  the context, in which the raise occured

        handlerContext   <Context>  the handlers (if found) context
        resumeBlock      <Block>    used during handler evaluation to get back
        rejectBlock      <Block>    used during handler evaluation to reject

    Class variables:
        EmergencyHandler <Block>    this block is evaluated, if no handler was defined
                                    for a signal (i.e. this one is responsible for the
                                    unhandled exception debugger).
                                    Having this being a block allows to globally catch
                                    these errors - even when no enclosing handler-scope
                                    around the erronous code exists.
                                    (as the catch/through does).
"
! !

!Exception class methodsFor:'defaults'!

emergencyHandler
    "return the handler used for unhandled exceptions"

    "
     set it up, when called the first time
    "
    EmergencyHandler isNil ifTrue:[
        EmergencyHandler := [:ex |
            (ex signal) enterDebuggerWith:(ex errorString).
        ]
    ].

    ^ EmergencyHandler
!

emergencyHandler:aOneArgBlock
    "set the handler used for unhandled exceptions"

    EmergencyHandler := aOneArgBlock
! !

!Exception methodsFor:'accessing'!

signal
    "return the signal, that caused the exception"

    ^ signal
!

parameter
    "return the parameter passsed with the signal raise
     (or nil, if there was none)"

    ^ parameter
!

errorString 
    "return the errorString passsed with the signal raise
     (or nil, if there was none)"

    ^ errorString
!

handlerContext
    "return the context of the handler"

    ^ handlerContext
!

suspendedContext
    "return the context in which the raise occured"

    ^ suspendedContext
!

rejected
    "return true, if the exception handler rejected.
     (only valid after handler execution)"

    ^ rejected
! !

!Exception methodsFor:'setup'!

signal:aSignal parameter:aParameter errorString:aString suspendedContext:sContext
    "set the fields usable for inspection by the handler
     - only to be sent from the signal when raising"

    signal := aSignal.
    parameter := aParameter.
    errorString := aString.
    suspendedContext := sContext.
!

handlerContext:aContext
    "set the context of the handler.
     - only to be sent from the signal when raising"

    handlerContext := aContext
!

rejectBlock:aBlock
    "this is meant to be sent by Signal only"

    rejected := false.
    rejectBlock := aBlock
!

resumeBlock:aBlock
    "this is meant to be sent by Signal only"

    resumeBlock := aBlock
! !

!Exception methodsFor:'handler actions'!

reject
    "handler decided not to handle this signal -
     system will look for another handler"

    rejected := true.
    rejectBlock value
!

resume
    "Continue after the raise - the raise returns nil"

    resumeBlock value:nil
!

resumeWith:value
    "Continue after the raise - the raise returns value"

    resumeBlock value:value
!

proceed
    "Continue after the raise - the raise returns nil"

    resumeBlock value:nil
!

proceedWith:value
    "Continue after the raise - the raise returns value"

    resumeBlock value:value
!

return
    "Continue after the handle:do: - the handle:do: returns nil"

    handlerContext unwind
!

returnWith:value
    "Continue after the handle:do: - the handle:do: returns value"

    handlerContext unwind:value
!

restart
    "restart the handle:do: - usually after some repair work is done
     in handler"

    handlerContext restart
! !