Numeric literals
Simon Marlow
simonmar@microsoft.com
Tue, 28 Aug 2001 15:53:05 +0100
> 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=20
> (e.g. if I'd
> put "af" instead of "45"), it seems to be pretty uncatchable,=20
> which is a
> pain. How do I tell if an instance of reading will work, or=20
> catch that it=20
> 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