小编Raf*_*eto的帖子

在Haskell中处理UserInterrupt异常

我正在为Haskell中的Scheme解释器实现一个REPL,我想处理一些像UserInterrupt,StackOverflow,HeapOverflow等异步事件......基本上,我想在UserInterrupt发生时停止当前的计算并打印一个StackOverflow和HeapOverflow发生时的合适消息等.我实现如下:

    repl evaluator = forever $ (do
        putStr ">>> " >> hFlush stdout
        out <- getLine >>= evaluator
        if null out
           then return ()
           else putStrLn out)
        `catch`
        onUserInterrupt

    onUserInterrupt UserInterrupt = putStrLn "\nUserInterruption"
    onUserInterrupt e = throw e

    main = do
        interpreter <- getMyLispInterpreter
        handle onAbort (repl $ interpreter "stdin")
        putStrLn "Exiting..."

    onAbort e = do
        let x = show (e :: SomeException)
        putStrLn $ "\nAborted: " ++ x
Run Code Online (Sandbox Code Playgroud)

它按预期工作,但有一个例外.如果我启动解释器并按Ctrl-Z + Enter,我会得到:

    >>> ^Z

    Aborted: <stdin>: hGetLine: end of …
Run Code Online (Sandbox Code Playgroud)

haskell exception-handling exception interruption read-eval-print-loop

10
推荐指数
1
解决办法
1457
查看次数