[fpc-pascal]exception handling
Thomas Schatzl
tom_at_work at gmx.at
Wed Mar 17 08:33:45 CET 2004
Subject: [fpc-pascal]exception handling
> spent a few moments going over the docs in the manual re: exception
> handling. for some reason it just doesn't seem to be sinking in, as
> nothing i've tried as of yet has managed to stop prog from crashing when
> data in input that's of a different type than program is expecting
> (string as opposed to integer, for example).
>
> as i said, i'm pretty sure that its something really small and obvious,
> i'll include a few lines here ... maybe someone can spot my mistake ...
> pretty sure its the EConvertError and that there's a pre-defined error
> type that I just haven't come across yet.
>
> try
> readln(NewAlbum.Year);
> except
> on EConvertError do NewAlbum.Year := 0;
> end;
- did you include the sysutils unit?
- afaik readln just raises runtime error which is unspecified according to
the docs; eventually an EIOError (or similar if this does not exist) because
this behaviour can be turned on/off with $I+/-.
But to be sure, you might want to catch general exceptions.
- do the following:
s: String;
i : Integer;
code : Word;
try
readln(s);
i := StrToInt(s);
catch
on EIOError ...
end;
or
{$I-}
readln(i);
{$I+}
if (IOResult <> 0) then ... // IOResult (hopefully) contains an error
number in case of an I/O error
or
readln(s);
val(s, i, code);
if (code <> 0) then Error...
But this has not been verified but it should be about right....
Regards,
Thomas
More information about the fpc-pascal
mailing list