Board index » delphi » I take exception to this Exception! - HELP

I take exception to this Exception! - HELP

I have some behavior with exceptions that I don't understand.  I'm using a
TEdit box to input a number that has to be an integer.  I wrote this
handler for the OnExit event:

procedure TFrm.EdtNExit(Sender: TObject);
begin
        inherited;
try
        N := StrToInt(EdtN.Text);
except
        on EConvertError do
                EdtN.Text := IntToStr(N);   // if not int, reset to previous value
        end;
end;

N is an integer that is set to 0 in the form's OnCreate event.

The problem is that when I input, say, 63.4 in the edit box, the program
halts with an EConvertError "Value is not an integer".  The way I
understand it from reading all the Delphi books is that the "try...except"
block is supposed to handle and clear the exception, and execution should
continue.

What am I doing wrong here?

--
Dr. Kenneth W. DeLong            | Phone 510-423-1506
Lawrence Livermore National Lab  | Fax 510-423-1488
L-395, Livermore CA 94550        | delo...@llnl.gov
Asonde kuraseru nara, sore wa sore de iin ja nai!!
Check out the FROG Web page at http://www.ca.sandia.gov/ultrafrog
Personal Page http://ourworld.compuserve.com/homepages/ken_delong

 

Re:I take exception to this Exception! - HELP


delo...@llnl.gov (Kenneth W. DeLong) wrote:
Huh

Quote
>procedure TFrm.EdtNExit(Sender: TObject);
>begin
>    inherited;
>try
>    N := StrToInt(EdtN.Text);

You just Clobbered N

Quote
>except
>    on EConvertError do
>            EdtN.Text := IntToStr(N);   // if not int, reset to previous value
>    end;
>end;
>N is an integer that is set to 0 in the form's OnCreate event.

No it isn't you just clobbered it 2 lines before.

Quote
>The problem is that when I input, say, 63.4 in the edit box, the program
>halts with an EConvertError "Value is not an integer".  The way I
>understand it from reading all the Delphi books is that the "try...except"
>block is supposed to handle and clear the exception, and execution should
>continue.
>What am I doing wrong here?
>--
>Dr. Kenneth W. DeLong            | Phone 510-423-1506
>Lawrence Livermore National Lab  | Fax 510-423-1488
>L-395, Livermore CA 94550        | delo...@llnl.gov
>Asonde kuraseru nara, sore wa sore de iin ja nai!!
>Check out the FROG Web page at http://www.ca.sandia.gov/ultrafrog
>Personal Page http://ourworld.compuserve.com/homepages/ken_delong

Tox
---------------------------------------------------------------------
Two men walk into a bar one after the other, you would have thought
that the second guy would have been smarter.
---------------------------------------------------------------------
This item was brought to you by the letters E and B and the number 7

Re:I take exception to this Exception! - HELP


In article <4spsmc$...@info.curtin.edu.au>,
   eeva...@cc.curtin.edu.au (Toxic) wrote:
]-delo...@llnl.gov (Kenneth W. DeLong) wrote:
]-Huh
]-
]->procedure TFrm.EdtNExit(Sender: TObject);
]->begin
]->  inherited;
]->try
]->  N := StrToInt(EdtN.Text);
]-
]-You just Clobbered N

did he?  I thought so at first, but now I'm convinced he didn't.

I'd guess that the exception raised by StrToInt triggers the
exception handling code BEFORE an assignment to N is made, and
that as a consequence N retains its original value.  Certainly
that's the way I'd want it to work...

]->except
]->  on EConvertError do
]->        EdtN.Text := IntToStr(N);   // if not int, reset to previous value
]->  end;
]->end;
]-
]->N is an integer that is set to 0 in the form's OnCreate event.
]-
]-No it isn't you just clobbered it 2 lines before.

I tried several different flavors of this code.  (N = global
typed constant, N = public field, etc.)  In every case of
invalid input N retained the value it had before the EConvertError.

Mark Vaughan

Re:I take exception to this Exception! - HELP


In article <delong2-190796083...@tsurugi.llnl.gov>,
   delo...@llnl.gov (Kenneth W. DeLong) wrote:
]-I have some behavior with exceptions that I don't understand.  I'm using a
]-TEdit box to input a number that has to be an integer.  I wrote this
]-handler for the OnExit event:
]-
]-procedure TFrm.EdtNExit(Sender: TObject);
]-begin
]-      inherited;
]-try
]-      N := StrToInt(EdtN.Text);
]-except
]-      on EConvertError do
]-            EdtN.Text := IntToStr(N);   // if not int, reset to previous value
]-      end;
]-end;
]-
]-N is an integer that is set to 0 in the form's OnCreate event.
]-
]-The problem is that when I input, say, 63.4 in the edit box, the program
]-halts with an EConvertError "Value is not an integer".  The way I
]-understand it from reading all the Delphi books is that the "try...except"
]-block is supposed to handle and clear the exception, and execution should
]-continue.
]-
]-What am I doing wrong here?
]-

Nothing really...

I'd guess that you're running this in the IDE, right?  If so,
then go to Options|Environment|Preferences, and uncheck the
"Break On Exceptions" box.  When this option is checked, Delphi
jumps in and warns you about any exceptions it encounters, by-
passing any exception handlers you may have written; when
unchecked, your exception handlers will take control instead.

Mark Vaughan

Re:I take exception to this Exception! - HELP


de>I have some behavior with exceptions that I don't understand.  I'm using a
de>TEdit box to input a number that has to be an integer.  I wrote this
de>handler for the OnExit event:
de>     N := StrToInt(EdtN.Text);
de>N is an integer that is set to 0 in the form's OnCreate event.
de>The problem is that when I input, say, 63.4 in the edit box, the program

You had better check in the manuals what an integer is. You may also want to
check out the Format function among others.

--
Mr. HillBilly (hillbi...@programmers.bbs.no)
Programmers BBS er oppe igjen, n? p? tlf. 22217666/22217669!
 tHyperSoft HyperReader 1.0t

Re:I take exception to this Exception! - HELP


Quote
Kenneth W. DeLong wrote:

> I have some behavior with exceptions that I don't understand.  I'm using a
> TEdit box to input a number that has to be an integer.  I wrote this
> handler for the OnExit event:

> procedure TFrm.EdtNExit(Sender: TObject);
> begin
>         inherited;
> try
>         N := StrToInt(EdtN.Text);
> except
>         on EConvertError do
>                 EdtN.Text := IntToStr(N);   // if not int, reset to previous value
>         end;
> end;

> N is an integer that is set to 0 in the form's OnCreate event.

> The problem is that when I input, say, 63.4 in the edit box, the program
> halts with an EConvertError "Value is not an integer".  The way I
> understand it from reading all the Delphi books is that the "try...except"
> block is supposed to handle and clear the exception, and execution should
> continue.
>    I bet you're executing this in the IDE. Try turning off "break on

exception" (you find that under Tools|Options... in D2.) What you're seeing
is the IDE's handling of the exception - then yours takes over.

        (EdtN.Text := IntToStr(N) is not going to restore the text to the
previous text, BTW. I don't know whether StrToInt(AString) is undefined
or has a standard error value when AString does not represent an integer,
but it certainly cannot be that IntTostr(StrToInt(AString))=AString in
that case.)

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:I take exception to this Exception! - HELP


delo...@llnl.gov (Kenneth W. DeLong) wrote:

Quote
>I have some behavior with exceptions that I don't understand.  I'm using a
>TEdit box to input a number that has to be an integer.  I wrote this
>handler for the OnExit event:
>procedure TFrm.EdtNExit(Sender: TObject);
>begin
>    inherited;
>try
>    N := StrToInt(EdtN.Text);
>except
>    on EConvertError do
>            EdtN.Text := IntToStr(N);   // if not int, reset to previous value
>    end;
>end;
>N is an integer that is set to 0 in the form's OnCreate event.
>The problem is that when I input, say, 63.4 in the edit box, the program
>halts with an EConvertError "Value is not an integer".  The way I
>understand it from reading all the Delphi books is that the "try...except"
>block is supposed to handle and clear the exception, and execution should
>continue.
>What am I doing wrong here?

First question, and apologies if you've already handled it yourself,
is:

Are you running the program in the Delphi IDE? If so, is what is
happening the work of the "break on exception" setting in
Options|Environment? Default for this is ON, which means that any
exception occurring in a prog run under the IDE (a) breaks and (b)
puts up an error message _before_ ever reaching your exception
handler. You have to turn off "break on exception" to let your own
handlers work.
--
John Nurick
j.nur...@dial.pipex.com

Other Threads