1 of 54

Condition Systems

in an

Exceptional Language

Clojure/Conj 2015

Chouser

2 of 54

Outline

  • Describe the problem
    • Exaggerate as needed
  • Describe a solution
    • The silver bullet
  • Introduce a new library implementing the solution
    • Give it a clever name
    • Describe only the positive attributes of the library
  • Never quite release the library

3 of 54

Actual Outline

  • Error handling options
  • Common Lisp’s condition system
  • Condition system libraries for Clojure
  • Clojure’s built-in condition “system”
  • Why none of these is a silver bullet�(but any of them may be useful)

4 of 54

5 of 54

6 of 54

“T

he action taken after detecting a software error (e.g., returning error codes) should be uniform for all components in the system. This leads to the difficult question of what action to take when an error is detected. The best action is immediately to terminate the program.

Myers, Glenford J. (1976) Software Reliability Principles & Practices

7 of 54

Error response options

Erlang,

Tru64 Unix

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

8 of 54

9 of 54

Error response options

Dramatic

Subtle

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

10 of 54

Error response options

Common Lisp

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

11 of 54

Error response options

Java NPE

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

12 of 54

Error response options

(Math/sqrt -2)

(first [])

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

13 of 54

Error response options

C’s errno

(agent-errors a)

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

14 of 54

Error response options

Linux “oops”

  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

15 of 54

Error response options

Who chooses response?

  • Detection site
  • Enclosing dynamic scope
  • Object creator or mutator
  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

16 of 54

Practical Common Lisp, Peter Seibel

17 of 54

18 of 54

19 of 54

20 of 54

21 of 54

Error response options

Who chooses response?

  • Detection site
  • Enclosing dynamic scope
  • Object creator or mutator
  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

22 of 54

Error response options

Who chooses response?

  • Detection site
  • Enclosing dynamic scope
  • Object creator or mutator
  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

23 of 54

24 of 54

Error response options

Who chooses response?

  • Detection site
  • Enclosing dynamic scope
  • Object creator or mutator
  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

25 of 54

26 of 54

Condition System

aka Resumable Exceptions / Conditions / Errors

aka Restarts

Common Lisp

slingshot & swell

not needed

define-condition

make-condition

error / signal

handler-case

restart-case

invoke-restart

throw+

handler-bind

restart-case

invoke-restart

27 of 54

Old:

New:

28 of 54

Old:

New:

29 of 54

30 of 54

31 of 54

Error response options

Who chooses response?

  • Detection site
  • Enclosing dynamic scope
  • Object creator or mutator
  • Crash
  • Enter debugger
  • Throw exception
  • Return special value
  • Set error flag
  • Recover, keep going

32 of 54

33 of 54

Condition system libraries

  • error-kit
  • slingshot + swell
  • ribol (now part of hara)
  • bwo/conditions

34 of 54

35 of 54

A composition problem

Awesome app

library A

library B

36 of 54

A composition problem

Awesome app

library A

library B

ribol

bwo/conditions

37 of 54

A composition problem

Awesome app

library A

library B

ribol

bwo/conditions

38 of 54

39 of 54

swell:

builtin:

40 of 54

...

41 of 54

42 of 54

43 of 54

44 of 54

*malformed-log-entry-error*

*malformed-entry-error*

*error*

*math-error*

*sqrt-of-negative*

45 of 54

46 of 54

Handler! Decline to handle!

47 of 54

48 of 54

hose who regularly code for fast electronic computers will have learned from bitter experience that a large fraction of the time spent in preparing calculations for the machine is taken up in removing the blunders that have been made in drawing up the programme. With the aid of common sense and checking subroutines the majority of mistakes are quickly found and rectified. Some errors, however, are sufficiently obscure to escape detection for a surprisingly long time.

“T

R. A. Brooker, S. Gill, and D. J. Wheeler, “The Adventures of a Blunder,”

Mathematical Tables and Other Aids to Computation, 6 (38), 112-113 (1952).

49 of 54

  • Consider alternatives to throwing exceptions

  • Try out the built-in condition system, knowing this technique can take you as far as you’re likely to need.

  • Use an existing condition system library where it makes sense

50 of 54

“V

ast opportunities that we software developers have to make models that prove and expand human knowledge, but we’re too busy plumbing.

51 of 54

References

Images:

Books:

  • Seibel, Peter (2005) Practical Common Lisp §19 http://www.gigamonkeys.com/book
  • Myers, Glenford J. (1976) Software Reliability Principles & Practices
  • Meyer, Bertrand (1997) Object-Oriented Software Construction §7.10, §9.3, §18.4
  • Gray, Jim & Reuter, Andreas (1992) Transaction Processing: Concepts and Techniques
  • Fogus & Houser The Joy of Clojure § Error Handling and Debugging

Papers:

  • R. A. Brooker, S. Gill, and D. J. Wheeler. (1952) The Adventures of a Blunder. Mathematical Tables and Other Aids to Computation, 6 (38), 112-113
  • Moreau, L. (1997) A syntactic theory of dynamic binding. TAPSOFT '97, 727-741
  • Felleisen, M., Friedman, D., Kohlbecker, E., & Duba, B. (1987) A syntactic theory of sequential control. Theoretical Computer Science, 205-237

52 of 54

53 of 54

Harvard Mark II, 1947

54 of 54

1943 - Colossus

1947 - moth

1952 - Brooker

1976 - Myers

1987 - Felleisen

1997 - Moreau

2007 - Clojure