0

我想知道如果以下代码引发异常,我如何捕获异常

query_3 <- quickQuery' conn_1 "SELECT MAX(high) \
                              FROM historicalData " []                                    
mapM_ (putStrLn . convertSqlValToString) query_3   

我知道可以使用名为 "catchSql" 的东西,但不知道如何在上面的代码中使用它

4

1 回答 1

1

I can't test it now, but try something like:

handleSql print $ do
    query_3 <- quickQuery' conn_1 "SELECT MAX(high) FROM historicalData" []
    mapM_ (putStrLn . convertSqlValToString) query_3

It uses

handleSql :: (SqlError -> IO a) -> IO a -> IO a

(which is just catchSql :: IO a -> (SqlError -> IO a) -> IO a with its arguments reversed).

Function handleSql runs the action given as its second argument, in your case quickQuery' followed by mapM_. And if a SqlError occurs during that part, it passes it to the function given as the first argument. So in the above example, if a SqlError occurs during the inner block, handleSql will call print on it..

于 2013-12-31T22:08:54.680 回答