Haskell Sqlite3 - Catching exceptions

I would like to know how I can catch an exception if raised by the following piece of code

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

I got to know that it's possible with something called "catchSql" , but have no idea how to use it in the above code

Answers


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..


Need Your Help

Limiting Form Submissions by Time

language-agnostic

This is not language specific. What are the best methods to limit how many times a person may submit a web form, not knowing any info about that person. Example: Voting for the all star game onli...

TextView scrolling and animation in Android

android scroll textview

I've successfully enabled scrolling for an Android TextView by either placing it within a ScrollView or, alternatively, using TextView's setMovementMethod (e.g., myTextView.setMovementMethod(new