Unformatted text preview:

ExceptionsError-handlingSlide 3ContextsSlide 5Key IdeaException HandlingSlide 8Slide 9Exception ProtocolMultiple HandlersSlide 12Exception HierarchyUnwind ProtectionSlide 15Slide 16An exampleThe Handle BlockPowerPoint PresentationSlide 20HaltSlide 22ExceptionsAn example of reflectionUsing objects to implement a languageError-handlingHow do you signal an error?1) Special return value2) Evaluate error block(map at: key ifAbsent: [ ^map at: key put: (Set with: value)]) add: value3) ExceptionsExceptionsModule will “raise an exception”Client will “handle an exception”In Smalltalk, exception is an object.Implementation uses contexts.Contexts• context stores temporaries, method arguments, program counter, and return address• thisContext gives method its own context• used by debugger, exception handling, processes, backtrackingContextsPolylineFigurePolylineFiguredisplayOn:displayOn:CompositeFigureCompositeFiguredisplayOn:displayOn:DrawingDrawingdisplayOn:displayOn:Key IdeaProgram handles a particular error signal.[ object doWork ]on: Error do: [ :theException | ... ]Server raises the same class of exception.Error signalException Handling[ x / y ] on: ZeroDividedo: [ :theException | theException return: 0]/ signals an error by ZeroDivide signalException HandlingThe ”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.ExceptionsExceptions can have an error string.Some implementations give them other arguments, but that is not part of the standard.Exceptionsignalsignal: aMessageStringException Protocolresume: - return from message that signaled exceptionretryUsing: - abort exception handler and reevaluate its exception blockreturn: - return from block protected by active exception handlerMultiple HandlersException travels down stack of contexts, looking for a handler to the exception. It evaluates the handle block of the first match.......E raiseE raiseon: E do:on: E do:......on: E: do:on: E: do:Exception Protocolreject will cause an Exception to look for the next handler.return will throw away all contexts above the handler.Exception HierarchyHandler for one exception will catch all subclasses, as well.ExceptionErrorArithmeticErrorMessageNotUnderstoodKeyNotFoundErrorUnwind ProtectionProblem: 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.Unwind ProtectionSolution: [self dangerousCode] ifCurtailed: [self cleanUp]The cleanup block is used if the execution stack is cut back for any reason.Unwind Protectionensure: evaluates the cleanup block after the receiver is evaluated.Semaphore critical: aBlockself wait.aBlock ensure: [self signal]An exampleprocessFile: aFile"Read a catalog card from the first comment. Trap and report all errors."aFile isReadable ifFalse: [^self]. [self processWithErrorsFile: aFile]on: Errordo: [:ex | ... ]The Handle Block[:ex | Transcript show: ex errorString. Transcript show: ' for ' , aFile asString.Transcript cr.Dialog confirm: ex errorString.ex return ]processWithErrorsFile: aFile "Read a catalog card from the first comment."| aStream string document |aStream := aFile readStream.[...] ensure: [aStream close]If we encounter an error [Transcript show: 'bad file ' , aFile asString.^Transcript cr].Also, system generates errors for files that are not readable.Halthalt"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 signalException handling important in developing reusable libraries.Squeaks reflective use of contexts makes it possible to implement exception handling as a class


View Full Document

UIUC CS 598 - Exceptions

Download Exceptions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?