Help with Exceptions on I/O

Alexandre Weffort Thenorio alethenorio@home.se
Tue, 12 Aug 2003 19:20:36 +0200


I have a program which creates textfiles out of other files. Since the
program is runned from windows I output some text strings (Like "File
created succefully") and I need to stop the program before it quits so that
the user can read the line outputted to know what went on and then he can
press ENTER to quit the program.

I managed to do this fine if no error occurs but when a error occurs I am
having problems.

The code goes like that

main :: IO()
main =catch (do
                     Do all the needed stuff here
                     putStr "File created Successfully. Press RETURN to
quit"
                     dummy <- getLine --Halts the program so the user can
read the above line)
                     putStr "Exiting now..." --needed since I can't finish a
do function with the "dummy<- getLine" line) (\_ -> do putStr "\nFile not
found. Press RETURN (ENTER) to quit."
     dumb <- getLine
     putStr "\nExiting...")

So when the program runs, if the input file is found the program writes
successfull creation of file but if the file doesn't exist, after the user
gives the input filename and press enter, the program creates a new line and
Halts (Probably because of the getLine function) without writing out
anything, then when the user press ENTER again it writes the line at the
first putStr (File not...), then writes the next putStr line under it
(Exiting...) and exits. I don't know why it doesn't wirte the first line,
halts and then when user press enter it writes the second and quits.

Can anybody help me as I am not very familiar with exception and catches.


Another question I have is: Is there any other function rather than getLine
that halts a program and continues when a user press any key (Instead of
ENTER) and besides this is an ugly code since getLine wasn't made for that
but I couldn't find anything else myself.

Thank you in advance.

Best Regards

Alex