Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-08

exception handling issue

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

kractor

having some problems with exception handling that have me pretty confused .... i'll include below the entire procedure where the exception handling occurs ...

     Procedure procAddNewAlbum;
         Var
             tmpAlbumName : AnsiString;
             tmpAlbumID : Longint;
             tmpNumTracks : Smallint;
             tmpLabel : AnsiString;
             tmpYear : Integer;
         Begin
             Write ('album name: ');
             ReadLn (tmpAlbumName);
             tmpAlbumID := ID_Generator;
             Write ('tracks: ');
             Try
                 ReadLn (tmpNumTracks);
             Except
                 On E: EInOutError do tmpNumTracks := 0;
             End;
             WriteLn ('tmpNumTracks = ',tmpNumTracks);
             Write ('label: ');
             ReadLn (tmpLabel);
             Write ('year: ');
             Try
                 ReadLn (tmpYear);
             Except
                 tmpYear := 0;
             End;
         End;

now, if i enter anything for tmpNumTracks that's not numeric (and therefore raises the exception) the following happens ...

album name: test
tracks: g
tmpNumTracks = 0
label: year: ^C

the exception code appears to be executed, but when the program "exits" the exception block it seems to be processing one line, then skipping one, then proceeding as normal ... i don't *think* i've made any syntax mistakes, and if i have i'm going to look rather silly ... :D

Reply by email

Michalis Kamburelis
to kractor

Hi

Your problem is the consequence of how ReadLn(Longint) works. When you enter 'g' + Enter, ReadLn reads 'g', then ReadLn raises runtime error (this causes EInOutError exception), but Enter is still left unread (it stays in internal Input buffer). That's why the next ReadLn ("ReadLn (tmpLabel);") returns immediately (and sets tmpLabel to '').

Simplest workaround is to not use ReadLn(Longint), use only ReadLn(String) and do convertion String -> LongInt explicitly. Like this:

   tmpNumTracksStr: AnsiString;
   ...
   ReadLn (tmpNumTracksStr);
   Try
     tmpNumTracks := StrToInt(tmpNumTracksStr);
   Except
     On E: EConvertError do tmpNumTracks := 0;
   End;
   ...

or even simpler:

   ReadLn (tmpNumTracksStr);
   tmpNumTracks := StrToIntDef(tmpNumTracksStr, 0);
Michalis kractor wrote: > having some problems with exception handling that have me pretty > confused .... i'll include below the entire procedure where the > exception handling occurs ... > > Procedure procAddNewAlbum; > Var > tmpAlbumName : AnsiString; > tmpAlbumID : Longint; > tmpNumTracks : Smallint; > tmpLabel : AnsiString; > tmpYear : Integer; > Begin > Write ('album name: '); > ReadLn (tmpAlbumName); > tmpAlbumID := ID_Generator; > Write ('tracks: '); > Try > ReadLn (tmpNumTracks); > Except > On E: EInOutError do tmpNumTracks := 0; > End; > WriteLn ('tmpNumTracks = ',tmpNumTracks); > Write ('label: '); > ReadLn (tmpLabel); > Write ('year: '); > Try > ReadLn (tmpYear); > Except > tmpYear := 0; > End; > End; > > now, if i enter anything for tmpNumTracks that's not numeric (and > therefore raises the exception) the following happens ... > > album name: test > tracks: g > tmpNumTracks = 0 > label: year: ^C > > the exception code appears to be executed, but when the program "exits" > the exception block it seems to be processing one line, then skipping > one, then proceeding as normal ... i don't *think* i've made any syntax > mistakes, and if i have i'm going to look rather silly ... :D > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >

Reply by email