14 - Exceptions

563 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
563
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

14 - Exceptions

  1. 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Exceptions...a simple introduction
  2. 2. S.Ducasse 2 Standardized by ANSI in 1996 Exception is the root of the exception hierarchy: 84 predefined exceptions.The two most important classes are: Error Notification Specialised into predefined exceptions -> subclass them to create your own exceptions Some methods of Exception: defaultAction is executed when an exception occurs description string describing the actual exception Exceptions
  3. 3. S.Ducasse 3 |x y| x := 7. y := 0. [x/y] on: ZeroDivide do: [:exception| Transcript show: exception description, cr. 0....] Catching an Exception
  4. 4. S.Ducasse 4 Signaling an Exception Error signal Warning signal: ‘description of the exception’
  5. 5. S.Ducasse 5 Exception an Exception Handler is defined using on:do: and is composed of an exception class (ZeroDivide) and a handler block [:theException| Transcript show:‘ division by zero’] An Exception Handler completes by returning the value of the handler block in place of the value of the protected block (here [x/y]). We can exit the current method by putting an explicit return inside the handler block
  6. 6. S.Ducasse 6 The Main Exceptions ofVW Exception class Exceptional Event Default Action Error Any program error Open a Notifier a Notifier ArithmeticError Any error evaluating an arithmetic Inherited from Error MessageNotUnderstood Any unusual event that does not impair continued execution of the program Inherited from Error Notification Notification Any unusual event that does not impair continued execution of the program Do nothing continuing executing Do nothing continuing executing Warning An unusual event that the user should be informed about Display Yes/No dialog and return a boolean value to the signaler
  7. 7. S.Ducasse 7 Exception Sets [do some work] on: ZeroDivide, Warning do: [ :ex| what you want] Or |exceptionSets| exceptionSets := ExceptionSet with: ZeroDivide with:Warning. [do some work] on: exceptionSets do: [ :ex| what you want] Exception Sets
  8. 8. S.Ducasse 8 Each process has its own exception environment: an ordered list of active handlers. Process starts -> list empty [aaaa] on: Error do: [bbb] -> Error,bbb added to the beginning of the list When an exception is signaled, the system sends a message to the first handler of the exception handler. If the handler cannot handle the exception, the next one is asked If no handler can handle the exception then the default action is performed Exception Environment 8
  9. 9. S.Ducasse 9 A handler block completes by executing the last statement of the block.The value of the last statement is then the value returned by the handler block. Where this value should be returned depends: Nonresumable (Error) Sq: ([Error signal.‘Value from protected block’] on: Error do: [:ex|ex return: ‘Value from handler’]) > ‘Value from handler’ Resumable and Non-Resumable (i)
  10. 10. S.Ducasse 10 Resumable (Warning, Notification) In this case Notification signal raises an exception, then the context is restored and the value returned normally [Notification raiseSignal.‘Value from protected block’] on: Notification do: [:ex|ex resume: ‘Value from handler’] [Notification signal. 'Value from protected block'] on: Notification do: [:ex|ex resume: 'Value from handler'] > ‘Value from protected Block’. Resumable and Non-Resumable (ii)
  11. 11. S.Ducasse 11 Transcript show: [Notification raiseSignal. 'Value from protected block'] on: Notification do: [:ex| Transcript show: 'Entering handler '. 'Value from handler'. '5'] -> Entering handler 5 Resume:/Return:
  12. 12. S.Ducasse 12 Transcript show: [Notification raiseSignal. 'Value from protected block'] on: Notification do: [:ex| Transcript show: 'Entering handler '. ex resume: 'Value from handler'. '5'] > Entering handlerValue from protected block Transcript show: [Notification raiseSignal. 'Value from protected'] on: Notification do: [:ex| Transcript show: 'Entering handler '. ex return: 'Value from handler'. '5'] -> Entering handlerValue from handler Resume:/Return:
  13. 13. S.Ducasse 13 exit or exit: (VW specific) Resumes on a resumable and returns on a nonresumable exception resume or resume:Attempts to continue processing the protected block, immeditely following the message that triggered the exception. return or return: ends processing the protected block that triggered the exception retry re-evaluates the protected block retryUsing: evaluates a new block in place of the protected block Exiting Handlers Explicitly
  14. 14. S.Ducasse 14 resignalAs: resignal the exception as another on pass exit the current handler and pass to the next outer handler, control does not return to the passer outer as with pass, except will regain control if the outer handler resumes exit:, resume: and return: return their argument as the return value, instead of the value of the final statement of the handler block Exiting Handlers Explicitly (ii)
  15. 15. S.Ducasse 15 Look in Exception class examples categories -2.0 to: 2.0 do: [ :i | [ 10.0 / i.Transcript cr; show: i printString ] on: Number divisionByZeroSignal do: [:ex | Transcript cr; show: 'divideByZero abort'. ex return ] ] -2.0 -1.0 divideByZero abort 1.0 2.0 Examples
  16. 16. S.Ducasse 16 retry recreates the exception environment of active handlers [ x /y] on: ZeroDivide do: [:exception| y := 0.00001. exception retry] Examples

×