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

the bright side of exceptions
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

The bright side of exceptions

The Lisp Condition System Didier Verna

didier@lrde.epita.fr http://www.lrde.epita.fr/˜didier http://www.facebook.com/didierverna @didierverna

ACCU 2013 – Saturday, April 13th

1/20

slide-2
SLIDE 2

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

What is an exception?

The JavaTM Tutorials (docs.oracle.com) Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. When an error occurs [. . . ] If the runtime system exhaustively searches all the methods

  • n the call stack without finding an appropriate exception

handler[. . . ], the runtime system[. . . ] terminates. ◮ Unfortunately, “exception” really means “error”.

2/20

slide-3
SLIDE 3

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

Benefits of exception-based error handing

  • 1. Separation of concerns
  • pen ( f i l e ) ;

i f ( opened ) { parse ( stream ) ; i f ( parsed ) { i n t e r p r e t ( contents ) ; i f ( i n t e r p r e t e d ) act ( ) ; else return i n t e r p r e t a t i o n _ e r r o r ; } else return parse_error ; } else return

  • pen_error ;

try {

  • pen ( f i l e ) ;

parse ( stream ) ; i n t e r p r e t ( contents ) ; } catch {

  • pen_error :

handle_it ( ) ; parse_error : handle_it ( ) ; i n t e r p r e t a t i o n _ e r r o r : handle_it ( ) ; }

  • 2. Up-stack propagation
  • 3. (OO-like) Hierarchies
  • 4. User-defined exceptions

3/20

slide-4
SLIDE 4

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

Drawbacks of the usual try/catch/throw model

1 Mandatory stack unwinding

Execution context is lost

2 2D Separation of Concerns

◮ Throwing an exception ◮ Handling it

Hardwired handler selection

4/20

slide-5
SLIDE 5

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

Table of contents

1

Basic Error Handling Catching and throwing errors Defining errors

2

Advanced Error Handling Restarts Dynamic error management

3

Beyond Error Handling Example 1: warnings Example 2: general signals

5/20

slide-6
SLIDE 6

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

Basic error handling

Equivalent to try/catch

( handler−case form ; ; t r y ( error1 ( var1 ) form1 ) ; ; catch ( error2 ( var2 ) form2 ) . . . )

Equivalent to finally

( unwind−protect form cleanup ; ; f i n a l l y . . . )

Non-local transfer to an exit point

◮ tagbody/go ◮ catch/throw 7/20

slide-7
SLIDE 7

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

User-defined errors

User-level definitions

( define−condition name ( supers . . . ) ( ( slot−name options . . . ) ( slot−name options . . . ) . . . )

  • ptions . . . )

User-level throwing

( error datum arguments . . . )

Jargon:

◮ We don’t “throw an error” ◮ We “signal a condition” 8/20

slide-8
SLIDE 8

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

3D Separation of Concerns

No mandatory stack unwinding

10/20

slide-9
SLIDE 9

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

User-defined restarts

Restart definition

( restart−case form ( r e s t a r t 1 ( args . . . )

  • ptions . . .

body ) ( r e s t a r t 2 ( args . . . )

  • ptions . . .

body ) . . . )

11/20

slide-10
SLIDE 10

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

Restarts management

Restart invocation

( invoke−restart r e s t a r t args . . . )

Stack-preserving handlers

( handler−bind ( ( error1 handler−function ) ; ; you may c a l l ( error2 handler−function ) ; ; invoke−restart . . . ) ; ; here . . . body )

12/20

slide-11
SLIDE 11

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

Dynamic error management

Restart definition

( restart−case form ( r e s t a r t ( args . . . )

  • ptions . . .

body ) . . . )

Conditional availability

; ; The : t e s t

  • ption

to r e s t a r t s ( find−restart r e s t a r t ) ; ; Is r e s t a r t available ? ( compute−restarts ) ; ; Get a l l available r e s t a r t s

Interactive calling

; ; The : i n t e r a c t i v e

  • ption

to r e s t a r t s ( invoke−restart−interactively r e s t a r t )

13/20

slide-12
SLIDE 12

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

The truth about error

The function error:

1 signals a condition 2 looks for an appropriate handler

  • may handle the error (non-local exit)
  • may not (decline by returning)

3 eventually invokes the debugger

The function cerror:

◮ “continuable error” ◮ invokes the debugger but. . . ◮ . . . establishes a restart named continue

◮ What about not invoking the debugger at all ?

15/20

slide-13
SLIDE 13

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

Example 1: Warnings

The function warn:

1 signals a condition 2 looks for an appropriate handler

  • may handle the error (non-local exit)
  • may not (decline by returning)

3 eventually prints the condition and returns

Also establishes a muffle-warning restart

16/20

slide-14
SLIDE 14

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

The Truth about warn

The true truth about error

The function warn:

1 establishes a muffle-warning restart 2 calls signal to signal the condition 3 eventually prints the condition and returns

The function [c]error:

1 [establishes a continue restart] 2 calls signal to signal the condition 3 eventually invokes the debugger

The function signal:

1 looks for an appropriate handler

  • may handle the error (non-local exit)
  • may not (decline by returning)

2 simply returns otherwise

◮ User-defined protocols on top of signal

17/20

slide-15
SLIDE 15

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

Example 2: coroutines (sort of) and yield

Generators / iterators, really

Coroutines:

◮ “yield” values ◮ retain their

  • state
  • position

Generator example

( defun squares ( ) ( loop : f o r i : from 0 : do ( y i e l d (∗ i i ) ) ) )

Retain state and position ⇐ ⇒ don’t unwind the stack yield values ⇐ ⇒ signal them The trick:

1 yield values by signalling a condition 2 use them in a condition handler (by side-effect) 3 let the handler return normally (pretend it declined) !!

18/20

slide-16
SLIDE 16

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

The big picture

Worth a thousand words

19/20

slide-17
SLIDE 17

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

Conclusion

There is more to exceptions than meets the eye. . . Traditional approach is limited

◮ Stack unwinding ◮ 2D separation of concerns

Improvements

◮ No (mandatory) stack unwinding ◮ 3D separation of concerns

◮ Better error management ◮ No restricted to errors ◮ Not even restricted to exceptions ◮ User-defined condition-based protocols

20/20