[Haskell-cafe] Using catch and errors

Alexandre Weffort Thenorio alethenorio at home.se
Fri Apr 22 11:35:23 EDT 2005


>Try:
>
>main :: IO ()
>main =
>  do removeFile "./newint.hex"
>    hexFile <- catch (readFile "int.hex")  (\_ -> error "Cannot find
>nt.hex")
>    getLine
>    return ()

That works fine indeed the only problem is that there is no stop so if the
user runs it on windows from Windows Explorer, for example, the program will
simply open and close (In case the error occurs), and the user won't ever
find out what was the problem. Is there any way to make it stop so the user
can read the message and has to press enter to quit?

>if/then/else' expressions _must_ have an 'else' part. In this case
>you can use 'Control.Monad.when': 'when ex (removeFile "newint.hex")'.

Works like a charm, thanks a lot.


>You can use pattern matches and guards for that.

Can I really use guards on a IO function? I thought that wasn't possible,
cause I need do and upon guard use no more than one line per guard is
allowed or am I wrong? I guess I can try catch


>I'm not sure what you use 'keyno' for but perhaps you could define a
>'data Keyno = Zero | One' data type and catch such errors at
>compile-time?

keyno is simply a string, which will contains either "0" or "1" (Sure the
user can give other stuff but anything else will be regarded as a 1
therefore I wanted to assert the user puts either 0 or 1 giving an error
otherwise). But I managed to solve everything with "when" functions, since
keyno and key are being asked in main I simpled check what I need and give
error otherwise.

The only problem now is to figure out how to make the program stop to allow
user to read the error before quiting. (Maybe I can override the "error"
function, but I don't remember how to make it use my implementation).

Thanks a lot., it sure helped.


>I've heard "Yet Another Haskell Tutorial" should be good but I've
>never read it myself. You can download it from
>http://www.isi.edu/~hdaume/htut/

Thanks, I also heard about it but haven't read yet.

Best Regards

NooK



More information about the Haskell-Cafe mailing list