SlideServe
Menu
Préférences
© SlideServe · v2026

Exceptions

lizina avatar
22 diapositives · 434 Views · Mar 24, 2012

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.

Exceptions - slide 1
01 / 22Exceptions
1 / 17

Télécharger la présentation

Exceptions
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.