1 / 22

Exceptions

Exceptions. An example of reflection Using objects to implement a language. Error-handling. How do you signal an error? 1) Special return value 2) Evaluate error block (map at: key ifAbsent: [ ^map at: key put: (Set with: value)]) add: value 3) Exceptions. Exceptions.

lizina
Download Presentation

Exceptions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exceptions An example of reflection Using objects to implement a language

  2. Error-handling • How do you signal an error? • 1) Special return value • 2) Evaluate error block • (map at: key ifAbsent: [ ^map at: key put: (Set with: value)]) add: value • 3) Exceptions

  3. Exceptions • Module will “raise an exception” • Client will “handle an exception” • In Smalltalk, exception is an object. • Implementation uses contexts.

  4. Contexts • • context stores temporaries, method arguments, program counter, and return address • • thisContext gives method its own context • • used by debugger, exception handling, processes, backtracking

  5. Contexts PolylineFigure displayOn: CompositeFigure displayOn: Drawing displayOn:

  6. Key Idea • Program handles a particular error signal. • [ object doWork ] • on: Error • do: [ :theException | ... ] • Server raises the same class of exception. • Error signal

  7. Exception Handling • [ x / y ] • on: ZeroDivide • do: [ :theException | theException return: 0] • / signals an error by • ZeroDivide signal

  8. Exception Handling • The ”signal" method of Exception • 1) creates an Exception, • 2) looks on the stack for an on:do: message that uses the class or its subclass, and • 3) evaluates the second argument of the on:do: message with the Exception as the argument.

  9. Exceptions • Exceptions can have an error string. • Some implementations give them other arguments, but that is not part of the standard. • Exception • signal • signal: aMessageString

  10. Exception Protocol • resume: - return from message that signaled exception • retryUsing: - abort exception handler and reevaluate its exception block • return: - return from block protected by active exception handler

  11. Multiple Handlers • Exception travels down stack of contexts, looking for a handler to the exception. It evaluates the handle block of the first match. E raise ... on: E do: ... on: E: do:

  12. Exception Protocol • reject will cause an Exception to look for the next handler. • return will throw away all contexts above the handler.

  13. Exception Hierarchy • Handler for one exception will catch all subclasses, as well. • Exception • Error • ArithmeticError • MessageNotUnderstood • KeyNotFoundError

  14. Unwind Protection • Problem: program can get blown away by an exception while it is in the middle of making delicate changes. This is a problem with returns in general, even without exceptions.

  15. Unwind Protection • Solution: • [self dangerousCode] ifCurtailed: [self cleanUp] • The cleanup block is used if the execution stack is cut back for any reason.

  16. Unwind Protection • ensure: evaluates the cleanup block after the receiver is evaluated. • Semaphore critical: aBlock • self wait. • aBlock ensure: [self signal]

  17. An example • processFile: aFile • "Read a catalog card from the first comment. Trap and report all errors." • aFile isReadable ifFalse: [^self]. • [self processWithErrorsFile: aFile] • on: Error • do: [:ex | ... ]

  18. The Handle Block • [:ex | • Transcript show: ex errorString. • Transcript show: ' for ' , aFile asString. • Transcript cr. • Dialog confirm: ex errorString. • ex return ]

  19. processWithErrorsFile: aFile • "Read a catalog card from the first comment." • | aStream string document | • aStream := aFile readStream. • [...] ensure: [aStream close]

  20. If we encounter an error • [Transcript show: 'bad file ' , aFile asString. • ^Transcript cr]. • Also, system generates errors for files that are not readable.

  21. Halt • halt • "This is the typical message to use for inserting breakpoints during debugging. It behaves like halt:, but does not call on halt: in order to avoid putting this message on the stack. Halt is especially useful when the breakpoint message is an arbitrary one." • Halt signal

  22. Exception handling important in developing reusable libraries. • Squeaks reflective use of contexts makes it possible to implement exception handling as a class library.

More Related