[fpc-pascal]exception handling

kractor baron.kractor at cogeco.ca
Wed Mar 17 15:55:32 CET 2004

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;
>fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
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 

            on E : EInOutError do NewAlbum.Year := 0;
        write('label: ');
        write('tracks: ');

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.

More information about the fpc-pascal mailing list