[Haskell-cafe] cannot catch exception

Kees Bleijenberg K.Bleijenberg at lijbrandt.nl
Mon Mar 4 20:26:41 UTC 2019


David, 

 

I tried your first suggestion $!. Nothing changed.

When I tried ‘Right <$> evaluate (TE.decodeUtf8 bufferStrict)’ success. handleException catches the exception. 

I don’t understand why. Maybe the documentation for the evaluate function below has to do with it:

There is a subtle difference between  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:evaluate> evaluate x and  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#v:return> return  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:-36--33-> $! x, analogous to the difference between  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:throwIO> throwIO and  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:throw> throw. If the lazy value x throws an exception,  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#v:return> return  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:-36--33-> $! x will fail to return an  <http://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO.html#t:IO> IO action and will throw an exception instead.  <http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:evaluate> evaluate x, on the other hand, always produces an  <http://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO.html#t:IO> IO action; that action will throw an exception upon execution iff x throws an exception upon evaluation.

I don’t fully understand this, but evaluate works. Thanks!

 

Kees

 

readCDFile :: FilePath -> FilePath -> IO (Either String T.Text)

readCDFile baseDir fn = do

  catch ( do 

            buffer <- B.readFile (combine baseDir fn)  --reads strict the whole file

            let bufferStrict = B.toStrict buffer

return $ Right $! TE.decodeUtf8 bufferStrict    -- this doesn’t work   

Right <$> evaluate (TE.decodeUtf8 bufferStrict) –- this does    

liftM Right $ evaluate (TE.decodeUtf8 bufferStrict) – this works too   

         ) exceptionHandler

 

 

Van: David Fox [mailto:dsf at seereason.com] 



This fixes it by forcing the evaluation of the decode where it can be caught:

 

return $ Right $! TE.decodeUtf8 bufferStrict

 

or 

 

Right <$> evaluate (TE.decodeUtf8 bufferStrict)

 

<snip>



---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190304/26c2a6c4/attachment.html>


More information about the Haskell-Cafe mailing list