2014-03-24 32 views
1

我有一块软件,它写在Racket,我想使用一个非常非常简单的异常处理程序:当引发异常时,处理程序打印出消息并终止应用程序。摆脱未捕获的异常

我可以重现下面的玩具例子行为:

(define (body) 
    (begin 
    (displayln "First line") 
    (error "Some error") 
    (displayln "This line is not printed"))) 

(call-with-exception-handler (lambda (x) (displayln "Exception handler")) body) 

这段代码的输出是:

我希望它只是displayln表达后退出,在异常处理程序(即打印"Exception handler"的表达式)。我怎样才能做到这一点?

+0

_which_'displayln'表达后?你有两个 –

+0

感谢您的注意。我编辑了这个问题 – Aslan986

回答

4

试试这个:

(with-handlers ([exn:fail? (lambda (exn) 
          ; in case you need the error message 
          (displayln (exn-message exn)) 
          (displayln "Exception handler"))]) 
    (displayln "First line") 
    (error "Some error") 
    (displayln "This line is not printed")) 

它会打印:

First line 
Some error 
Exception handler