[Haskell-cafe] Using catch and errors

Daniel Fischer daniel.is.fischer at web.de
Fri Apr 22 10:49:58 EDT 2005


Am Freitag, 22. April 2005 16:40 schrieb Alexandre Weffort Thenorio:
> I am trying to get catch to work but I guess I am total newbie on this one.
>
> main :: IO()
> main =  do
>   removeFile "./newint.hex"
>   hexFile <- catch (readFile int.hex)  (\_ -> do putStrLn "Cannot find
> int.hex"
>         getLine --Simply stop program so user can read error message
>         return() --Quit)
since 'readFile int.hex' -- shouldn't that be 'readFile "int.hex"' ? -- is IO 
String, the catch-branch must return a String, too.

>  hexFile <- readFile "int.hex"
> --res of program goes here
> getLine
> return()
>

I suggest

main = do
   removeFile "./newint.hex"
   catch (do hexFile <- readFile "int.hex"
                  whatever you want to do now)
             (putStrLn "doesn't exist")

I think that'll work,

Daniel

>
> But it keeps giving me error on getLine. How can I quit a program in case
> of an error giving an error message?
>
> Also I tried something like
>
> main :: IO()
> main =  do
>             ex <- doesFileExist "./newint.hex"
>              if ex then removeFile "./newint.hex"
>
> But that won't work either. Any ideas? I'd like to catch different errors
> and quit program giving different error messages
>
> Last but not least I have the function
>
> outputLine keyno key orgFile = do
>     let part1 = getLeft keyno (orgFile!!1)
>     let part2 = getRight keyno (orgFile!!1)
>     let total = part1 ++ (map toUpper key) ++ part2
>     let checks = checksum (drop 1 total)
>     let final = total ++ checks ++ "\n"
>     newHexFile <- openFile "newint.hex" WriteMode
>      hPutStrLn newHexFile (orgFile!!0 ++ "\n" ++ final ++ unlines (drop 2
> orgFile))
>
> I'd like to be able to check for stuff such as size of key and wheter keyno
> is only 1 or 0 and quit program with error message otherwise but again
> couldn't quite get catch to work.
>
> Any reading tips about subject would be appreciated.
>
> Best Regards
>
> NooK
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list