[Haskell-beginners] better exception handling

Henk-Jan van Tuyl hjgtuyl at chello.nl
Thu Oct 24 22:09:14 UTC 2013


On Wed, 23 Oct 2013 23:11:32 +0200, Miro Karpis  
<miroslav.karpis at gmail.com> wrote:

> Please, can you help me with following?... I have a working code but am  
> not
> very happy with the error handling in "updateStocks" function. It does  
> what
> it should, but I would like to implement some kind of guards. Problem is
> that it always ends it some error + am not sure if I can use guards in
> inside defined variable. Pls. what would you suggest?
>
>
>
> httpExceptionHandler ::  HttpException -> IO L.ByteString
> httpExceptionHandler e = (putStrLn "Error: simpleHttp returned  
> exception")
>                          >> (return L.empty)
>
> getStocks :: String -> IO L.ByteString
> getStocks url = (simpleHttp url) `X.catch` httpExceptionHandler
>
> updateStocks :: IO String
> updateStocks = do
>   yqlResult <- getStocks testQuery
>   case yqlResult of x | x == L.empty -> return "return exception"
>                                | otherwise    -> return "here I will call
> another function"

I think you need to give more details to say anything about this. What is  
the indication that there is nothing more to download?

I have some suggestions for code improvement:
- you use more parentheses than necessary, download hlint from hackage, it  
can suggest improvements

- the code:
>   case yqlResult of x | x == L.empty -> return "return exception"
>                       | otherwise    -> return "here I will call another  
> function"

can be simplified to:
>   if yqlResult == L.empty    then return "return exception"
>     else return "here I will call another function"

or even:
>   return    if yqlResult == L.empty      then "return exception"
>       else "here I will call another function"


Regards,
Henk-Jan van Tuyl


-- 
Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


More information about the Beginners mailing list