[Haskell-cafe] cannot catch exception

Kees Bleijenberg K.Bleijenberg at lijbrandt.nl
Mon Mar 4 11:50:13 UTC 2019


Hi all,

 

The program reads lots of small Text files. readCDFile handles the encoding.
Below is the simplest version of readCDFile.

If I call readCDFile "/home/kees/freeDB/inputError/" "blah" (the file blah
does not exist) I get:

Left "MyError: /home/kees/freeDB/inputError/blah: openBinaryFile: does not
exist (No such file or directory)". The exception is caught by
exceptionHandler

If I call readCDFile "/home/kees/freeDB/inputError/" "67129209" I get
freeDB: Cannot decode byte '\xa0': Data.Text.Internal.Encoding.decodeUtf8:
Invalid UTF-8 stream. The exception is not caught by exceptionHandler (No
"MyError: " in front). The file 67129209 is indeed bad encoded.

I'am using SomeException. Still, this 'bad encoding exception' is not
caught. Why?

 

Kees

 

import qualified Data.Text as T

import System.FilePath.Posix

import qualified Data.Text.Encoding as TE

import qualified Data.ByteString.Lazy as B

import Prelude hiding (catch)

import Control.Exception

 

main :: IO ()

main = do

          res <- readCDFile "/home/kees/freeDB/inputError/" "67129209" 

          print res

 

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

readCDFile baseDir fn = do

  catch ( do 

            buffer <- B.readFile (combine baseDir fn)

            let bufferStrict = B.toStrict buffer

            return $ Right $ TE.decodeUtf8 bufferStrict

         ) exceptionHandler

 

exceptionHandler :: SomeException -> IO (Either String T.Text)

exceptionHandler e = do let err = show e

                        return $ Left $ "MyError: " ++ err

 

 



---
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/36af22fb/attachment.html>


More information about the Haskell-Cafe mailing list