2014-03-24 33 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