Skip to main content
Engineering LibreTexts

12.18: Chapter Summary

  • Page ID
    45988
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this chapter we saw how to use exceptions to signal and handle abnormal situations arising in our code.

    • Do not use exceptions as a control-flow mechanism. Reserve them for notifications and for abnormal situations. Consider providing methods that take blocks as arguments as an alternative to signaling exceptions.
    • Use protectedBlock ensure: actionBlock to ensure that actionBlock will be performed even if protectedBlock terminates abnormally.
    • Use protectedBlock ifCurtailed: actionBlock to ensure that actionBlock will be performed only if protectedBlock terminates abnormally.
    • Exceptions are objects. Exception classes form a hierarchy with the class Exception at the root of the hierarchy.
    • Use protectedBlock on: ExceptionClass do: handlerBlock to catch exceptions that are instances of ExceptionClass (or any of its subclasses). The handlerBlock should take an exception instance as its sole argument.
    • Exceptions are signaled by sending one of the messages signal or signal:. signal: takes a descriptive string as its argument. The description of an exception can be obtained by sending it the message description.
    • You can set a breakpoint in your code by inserting the message-send self halt. This signals a resumable Halt exception, which, by default, will open a debugger at the point where the breakpoint occurs.
    • When an exception is signaled, the runtime system will search up the execution stack, looking for a handler for that specific class of exception. If none is found, the defaultAction for that exception will be performed (i.e., in most cases the debugger will be opened).
    • An exception handler may terminate the protected block by sending return: to the signaled exception; the value of the protected block will be the argument supplied to return:.
    • An exception handler may retry a protected block by sending retry to the signaled exception. The handler remains in effect.
    • An exception handler may specify a new block to try by sending retryUsing: to the signaled exception, with the new block as its argument. Here, too, the handler remains in effect.
    • Notifications are subclass of Exception with the property that they can be safely resumed without the handler having to take any specific action.

    Acknowledgments. We gratefully acknowledge Vassili Bykov for the raw material he provided. We also thank Paolo Bonzini for the Smalltalk implementations of ensure: and ifCurtailed:. We thank Hernan Wilkinson, Lukas Renggli, Christopher Oliver, Camillo Bruni, Hernan Wilkinson and Carlos Ferro for their comments and suggestions.


    This page titled 12.18: Chapter Summary is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.