the bright side of exceptions
play

The bright side of exceptions Introduction The Lisp Condition - PowerPoint PPT Presentation

The bright side of exceptions Didier Verna The bright side of exceptions Introduction The Lisp Condition System Basic Error Handling Catching and throwing errors Defining errors Didier Verna Advanced Error Handling Restarts


  1. The bright side of exceptions Didier Verna The bright side of exceptions Introduction The Lisp Condition System Basic Error Handling Catching and throwing errors Defining errors Didier Verna Advanced Error Handling Restarts didier@lrde.epita.fr Dynamic error management http://www.lrde.epita.fr/˜didier Beyond Error http://www.facebook.com/didierverna Handling @didierverna Warnings Signals Conclusion ACCU 2013 – Saturday, April 13th 1/20

  2. What is an exception? The bright side of The Java TM Tutorials ( docs.oracle.com ) exceptions Didier Verna Definition : An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of Introduction the program’s instructions. Basic Error Handling Catching and throwing errors When an error occurs [. . . ] Defining errors Advanced Error Handling If the runtime system exhaustively searches all the methods Restarts Dynamic error on the call stack without finding an appropriate exception management Beyond Error handler[. . . ], the runtime system[. . . ] terminates. Handling Warnings Signals ◮ Unfortunately, “exception” really means “error”. Conclusion 2/20

  3. Benefits of exception-based error handing The bright side of 1. Separation of concerns exceptions Didier Verna open ( f i l e ) ; try i f ( opened ) { Introduction { open ( f i l e ) ; parse ( stream ) ; parse ( stream ) ; Basic Error i f ( parsed ) i n t e r p r e t ( contents ) ; Handling { } Catching and i n t e r p r e t ( contents ) ; catch throwing errors i f ( i n t e r p r e t e d ) { Defining errors act ( ) ; open_error : handle_it ( ) ; Advanced else parse_error : handle_it ( ) ; Error Handling return i n t e r p r e t a t i o n _ e r r o r ; i n t e r p r e t a t i o n _ e r r o r : handle_it ( ) ; } } Restarts else Dynamic error management return parse_error ; } Beyond Error else Handling return open_error ; Warnings Signals Conclusion 2. Up-stack propagation 3. (OO-like) Hierarchies 4. User-defined exceptions 3/20

  4. Drawbacks of the usual try/catch/throw model The bright side of exceptions 1 Mandatory stack unwinding Didier Verna Execution context is lost 2 2D Separation of Concerns Introduction Basic Error ◮ Throwing an exception Handling ◮ Handling it Catching and throwing errors Defining errors Hardwired handler selection Advanced Error Handling Restarts Dynamic error management Beyond Error Handling Warnings Signals Conclusion 4/20

  5. Table of contents The bright side of exceptions Basic Error Handling 1 Didier Verna Catching and throwing errors Introduction Defining errors Basic Error Handling Catching and throwing errors Advanced Error Handling 2 Defining errors Restarts Advanced Error Handling Dynamic error management Restarts Dynamic error management Beyond Error Beyond Error Handling Handling 3 Warnings Example 1: warnings Signals Conclusion Example 2: general signals 5/20

  6. Basic error handling The bright side of Equivalent to try/catch exceptions Didier Verna ( handler − case form ; ; t r y ( error1 ( var1 ) form1 ) ; ; catch Introduction ( error2 ( var2 ) form2 ) Basic Error Handling . . . ) Catching and throwing errors Defining errors Advanced Equivalent to finally Error Handling Restarts Dynamic error ( unwind − protect form management cleanup ; ; f i n a l l y Beyond Error Handling . . . ) Warnings Signals Conclusion Non-local transfer to an exit point ◮ tagbody/go ◮ catch/throw 7/20

  7. User-defined errors The bright side of User-level definitions exceptions Didier Verna ( define − condition name ( supers . . . ) ( ( slot − name options . . . ) Introduction ( slot − name options . . . ) Basic Error Handling . . . ) Catching and throwing errors options . . . ) Defining errors Advanced Error Handling Restarts User-level throwing Dynamic error management ( error datum arguments . . . ) Beyond Error Handling Warnings Signals Jargon: Conclusion ◮ We don’t “throw an error” ◮ We “signal a condition” 8/20

  8. 3D Separation of Concerns No mandatory stack unwinding The bright side of exceptions Didier Verna Introduction Basic Error Handling Catching and throwing errors Defining errors Advanced Error Handling Restarts Dynamic error management Beyond Error Handling Warnings Signals Conclusion 10/20

  9. User-defined restarts The bright side of Restart definition exceptions Didier Verna ( restart − case form ( r e s t a r t 1 ( args . . . ) options . . . body ) Introduction ( r e s t a r t 2 ( args . . . ) options . . . body ) Basic Error Handling . . . ) Catching and throwing errors Defining errors Advanced Error Handling Restarts Dynamic error management Beyond Error Handling Warnings Signals Conclusion 11/20

  10. Restarts management The bright side of Restart invocation exceptions Didier Verna ( invoke − restart r e s t a r t args . . . ) Introduction Basic Error Stack-preserving handlers Handling Catching and throwing errors ( handler − bind Defining errors Advanced ( ( error1 handler − function ) ; ; you may c a l l Error Handling ( error2 handler − function ) ; ; invoke − restart Restarts Dynamic error . . . ) ; ; here . . . management body ) Beyond Error Handling Warnings Signals Conclusion 12/20

  11. Dynamic error management The bright side of Restart definition exceptions Didier Verna ( restart − case form ( r e s t a r t ( args . . . ) options . . . body ) Introduction . . . ) Basic Error Handling Catching and throwing errors Defining errors Conditional availability Advanced Error Handling ; ; The : t e s t option to r e s t a r t s Restarts Dynamic error ( find − restart r e s t a r t ) ; ; Is r e s t a r t available ? management ( compute − restarts ) ; ; Get a l l available r e s t a r t s Beyond Error Handling Warnings Signals Interactive calling Conclusion ; ; The : i n t e r a c t i v e option to r e s t a r t s ( invoke − restart − interactively r e s t a r t ) 13/20

  12. The truth about error The bright The function error : side of exceptions 1 signals a condition Didier Verna 2 looks for an appropriate handler • may handle the error (non-local exit) Introduction • may not (decline by returning) Basic Error Handling 3 eventually invokes the debugger Catching and throwing errors The function cerror : Defining errors ◮ “continuable error” Advanced Error Handling ◮ invokes the debugger but. . . Restarts Dynamic error ◮ . . . establishes a restart named continue management Beyond Error Handling Warnings ◮ What about not invoking the debugger at all ? Signals Conclusion 15/20

  13. Example 1: Warnings The bright The function warn : side of exceptions 1 signals a condition Didier Verna 2 looks for an appropriate handler • may handle the error (non-local exit) Introduction • may not (decline by returning) Basic Error Handling 3 eventually prints the condition and returns Catching and throwing errors Also establishes a muffle-warning restart Defining errors Advanced Error Handling Restarts Dynamic error management Beyond Error Handling Warnings Signals Conclusion 16/20

  14. The Truth about warn The true truth about error The bright The function warn : side of exceptions 1 establishes a muffle-warning restart Didier Verna 2 calls signal to signal the condition 3 eventually prints the condition and returns Introduction The function [c]error : Basic Error Handling 1 [establishes a continue restart] Catching and throwing errors 2 calls signal to signal the condition Defining errors Advanced 3 eventually invokes the debugger Error Handling The function signal : Restarts Dynamic error management 1 looks for an appropriate handler Beyond Error • may handle the error (non-local exit) Handling • may not (decline by returning) Warnings Signals 2 simply returns otherwise Conclusion ◮ User-defined protocols on top of signal 17/20

  15. Example 2: coroutines (sort of) and yield Generators / iterators, really The bright side of exceptions Coroutines: Generator example Didier Verna ◮ “yield” values ( defun squares ( ) ◮ retain their Introduction ( loop : f o r i : from 0 • state Basic Error : do ( y i e l d ( ∗ i i ) ) ) ) • position Handling Catching and throwing errors Retain state and position ⇐ ⇒ don’t unwind the stack Defining errors Advanced yield values ⇐ ⇒ signal them Error Handling Restarts The trick: Dynamic error management 1 yield values by signalling a condition Beyond Error Handling 2 use them in a condition handler (by side-effect) Warnings 3 let the handler return normally (pretend it declined) !! Signals Conclusion 18/20

  16. The big picture Worth a thousand words The bright side of exceptions Didier Verna Introduction Basic Error Handling Catching and throwing errors Defining errors Advanced Error Handling Restarts Dynamic error management Beyond Error Handling Warnings Signals Conclusion 19/20

Recommend


More recommend