lisp里面的异常处理采用的是condition system,下面是这个condition system的演示:
最简单的
[1]> (/ 1 0)
*** - /: division by zero
The following restarts are available:
ABORT :R1 Abort main loop
Break 1 [2]> :R1
[3]>
这个是被零除状况,运行时发现这个状况,提示有ABORT重启可以选择,选择这个重启退出.下面有添加自定义的重启的例子.
捕捉状况并处理
[3]> (handler-case (/ 1 0) (error () "Dived by 0"))
"Dived by 0"
添加自定义启动
[4]> (restart-case (/ 1 0) (r () "Restart R") (rr () "Restart RR") )
*** - /: division by zero
The following restarts are available:
R :R1 R
RR :R2 RR
ABORT :R3 Abort main loop
Break 1 [5]> :R2
"Restart RR"
这里定义了R RR这两个重启,lisp可以定义多个重启,对一种状况可以采用多种恢复方案.
状况处理选择自定义启动
Break 2 [8]> (handler-bind ((error #'(lambda (c) (invoke-restart 'rr)))) (restart-case (/ 1 0) (r () "Restart R") (rr () "Restart RR")))
"Restart RR"