[fpc-pascal]exception handling

Michael Van Canneyt michael.vancanneyt at wisa.be
Wed Mar 17 17:43:43 CET 2004


On Wed, 17 Mar 2004, kractor wrote:

> Michael Van Canneyt wrote:
>
> >On Tue, 16 Mar 2004, kractor wrote:
> >
> >
> >
> >>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;
> >>
> >>
> >
> >This should be:
> >
> >
> >      try
> >           readln(NewAlbum.Year);
> >      except
> >          on E : EConvertError do NewAlbum.Year := 0;
> >      end;
> >
> >Michael.
> >
> >_______________________________________________
> >fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> >http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> >
> >
> >
> tried your suggestion and when i attempted to input string when prog
> expecting integer value, it gave following error
>
> An unhandled exception occurred at 0x004012CC :
> EInOutError : Invalid input
>
> so, i tried changing the EConvertError to EInOutError, little code paste
> follows
>
>         try
>             readln(NewAlbum.Year);
>         except
>             on E : EInOutError do NewAlbum.Year := 0;
>         end;
>         write('label: ');
>         readln(NewAlbum.alLabel);
>         write('tracks: ');
>         readln(NewAlbum.NumTracks);
>
> what's happening now is that when "invalid" data type is entered which
> triggers the exception handle, the program is "skipping" the
> readln(NewAlbum.alLabel) command and resuming apparently normal
> operations on the very next line [write('tracks: ');]. when valid data
> is entered, the program naturally doesn't trigger the exception handle
> and everything works normally.

I have been able to reproduce this. However, I'm not sure what the cause
it. My guess is that the end-of-line is not consumed before the
exception is raised, and therefore the

        readln(NewAlbum.alLabel);

line gets an empty input line. Delphi does consume the end-of-line, and
waits for the input of the label.

This is a very tricky issue, I think the core FPC list needs to discuss
it.

For the moment, I would do like this;

Var
  A : String;

begin
         readln(A);
         NewAlbum.Year:=StrToIntDef(A,0);
         write('label: ');
         readln(NewAlbum.alLabel);
         write('tracks: ');
         readln(NewAlbum.NumTracks);

This avoids the exception.

Michael.




More information about the fpc-pascal mailing list