
I also haven't yet worked out how to tell if a string is "read"able or not, yet - if (read "45")::Integer or whatever gives an error (e.g. if I'd put "af" instead of "45"), it seems to be pretty uncatchable, which is a pain. How do I tell if an instance of reading will work, or catch that it didn't?
In GHC, you can do this: import Exception do result <- catch (evaluate (read "foo" :: Int)) (\error -> ... ) but unfortunately read doesn't raise a useful exception (just error "Prelude.read: no parse"), so you can't filter it very easily. However I don't think that read is likely to raise any other exceptions. Cheers, Simon

On Tue, 28 Aug 2001, Simon Marlow wrote: (snip)
In GHC, you can do this:
import Exception
do result <- catch (evaluate (read "foo" :: Int)) (\error -> ... ) (snip)
I do like that. Is it likely to become standard someday, do you think? Errors like this should certainly be catchable in such a way; I was sad when I was first looking into this and found that 'catch' seemed to be inextricably linked to IO. -- Mark

In GHC, you can do this:
import Exception
do result <- catch (evaluate (read "foo" :: Int)) (\error -> ... )
Note that this works in Hugs too. Well, it does in the cvs repository version... [Personally, I'd write this code the way Patrik and use exceptions only for those cases which can't be handled any other way or where the overhead of threading exceptions around is too high.] -- Alastair Reid reid@cs.utah.edu http://www.cs.utah.edu/~reid/
participants (3)
-
Alastair David Reid
-
Mark Carroll
-
Simon Marlow