Exception 未被'捕获的异常;试试';
我正在使用并试图处理服务器死机的情况。根据文件: 与服务器的连接丢失: 在连接丢失的情况下,命令函数会抛出ConnectionLostException。它只能在runRedis之外捕获 因此,我假设我想要捕捉ConnectionOstException。然而,虽然我似乎能正确地抓住它,但它似乎也会上升到顶部,我不知道为什么。以下是一些代码(仅在GHCI中运行): 这给了我: 左Con:无连接 无连接Exception 未被'捕获的异常;试试';,exception,haskell,Exception,Haskell,我正在使用并试图处理服务器死机的情况。根据文件: 与服务器的连接丢失: 在连接丢失的情况下,命令函数会抛出ConnectionLostException。它只能在runRedis之外捕获 因此,我假设我想要捕捉ConnectionOstException。然而,虽然我似乎能正确地抓住它,但它似乎也会上升到顶部,我不知道为什么。以下是一些代码(仅在GHCI中运行): 这给了我: 左Con:无连接 无连接 因此,我得到了预期的左结果,但在异常进行到一半时,GHCI可能也捕获并显示了异常。这是不是因为
因此,我得到了预期的
左
结果,但在异常进行到一半时,GHCI可能也捕获并显示了异常。这是不是因为没有被评估的东西出了问题?就像John暗示的那样,似乎有什么东西将此消息打印到了stderr
考虑这个例子:
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent (threadDelay)
import Control.Exception
import Control.Monad
import Database.Redis
import System.Mem
tryR :: IO a -> IO (Either ConnectionLostException a)
tryR = try
main :: IO ()
main = do
conn <- connect defaultConnectInfo
loop conn
putStrLn $ "exiting gracefully after counting up some numbers"
performGC
forM_ [1..10] $ \i -> do
print i
threadDelay 10000 -- 0.05 seconds
where
loop conn = do
e <- tryR . (>>= evaluate) . runRedis conn $ ping
case e of
Right x -> do print x
threadDelay 1000000
loop conn
Left err -> do putStrLn $ "tryR caught exception: " ++ show err
{-#语言重载字符串}
导入控制。并发(线程延迟)
导入控制。异常
进口管制
导入数据库.Redis
导入系统.Mem
tryR::IO a->IO(连接或异常a)
tryR=try
main::IO()
main=do
康涅狄格州
打印i
线程延迟10000--0.05秒
哪里
环路连接=do
e>=评估)。runRedis conn$ping
案件e
右x->打印x
线程延迟1000000
环路连接器
左错误->执行putStrLn$“tryR捕获异常:”++显示错误
它打印:
Right Pong
Right Pong <-------------- after this I Ctrl-C the redis server
tryR caught exception: ConnectionLost
exiting gracefully after counting up some numbers
1
test: ConnectionLost
2
3
4
5
6
7
8
9
10
右乒乓球
对,我不明白。在我看来,一切都正常工作:您已将异常转换为易于检查的或值。您希望发生什么情况呢?在我看来,redis包除了抛出异常外,还打印了一个关于连接丢失的字符串。@DanielWagner我正在转换异常,但(据我所知)仍然看到它被GHCI处理,就好像它仍然被抛出一样。除非是像JohnL说的那样,redis本身正在打印消息。您可能可以通过检查进程的结束状态来检查异常是否仍被抛出或只是打印。我认为,出于这个原因,你必须编译你的测试,而不是在GHCI中运行它。此外,考虑打开一个问题来澄清它是HEDIS还是它的依赖之一。
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent (threadDelay)
import Control.Exception
import Control.Monad
import Database.Redis
import System.Mem
tryR :: IO a -> IO (Either ConnectionLostException a)
tryR = try
main :: IO ()
main = do
conn <- connect defaultConnectInfo
loop conn
putStrLn $ "exiting gracefully after counting up some numbers"
performGC
forM_ [1..10] $ \i -> do
print i
threadDelay 10000 -- 0.05 seconds
where
loop conn = do
e <- tryR . (>>= evaluate) . runRedis conn $ ping
case e of
Right x -> do print x
threadDelay 1000000
loop conn
Left err -> do putStrLn $ "tryR caught exception: " ++ show err
Right Pong
Right Pong <-------------- after this I Ctrl-C the redis server
tryR caught exception: ConnectionLost
exiting gracefully after counting up some numbers
1
test: ConnectionLost
2
3
4
5
6
7
8
9
10