* * *

Author Topic: [SOLVED] StrToInt: Problems handling exceptions.  (Read 4482 times)

khf

  • New member
  • *
  • Posts: 10
[SOLVED] StrToInt: Problems handling exceptions.
« on: December 16, 2010, 05:25:33 pm »
Hi guys!

The idea is to have a link between a scrollbar and a edit box, so, if the user clicks on the scrollbar, the edit box will show the scrollbar's position, and if the user changes the edit box's text, the scrollbar will get the edit box value

The problem appears whenever the user let the textbox empty (before putting any value) or when the user puts a char.
 
What i'm using is the StrToInt function, and searching on the internet, i found there is a "try..except" clause that can help me with this problem.

My code is the next (i'm using a button to made the operation only for debugging purpose)
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  aux:integer;
begin
  Try
    aux:=StrToInt(edit1.Text);
    scrollbar1.Position:=aux;
  except On Exception do
    scrollbar1.Position:=0;
  end;

end;

If i put a number on the edit box, the program runs with no problem, but if i put a char, or i let the edit box empty, when the program counter reaches the line with the "strtoint" function, a message box appears telling me that
"project raised exception class 'EConvertError' with message....."

I think i may be missing some instructions to tell the computer to continue if there is any error.

I know that the main problem is when i let the edit box empty, and there is a simple solution:
Code: [Select]
if edit1.text <> '' then
begin
  [...]
end

But i want to learn how to handle exceptions in Lazarus, it can be usefull.

PS: Yes, i'm using
Code: [Select]
uses
  ... , SysUtils, ...;
« Last Edit: December 16, 2010, 06:56:11 pm by khf »

typo

  • Hero Member
  • *****
  • Posts: 1368
Re: StrToInt: Problems handling exceptions.
« Reply #1 on: December 16, 2010, 05:34:41 pm »
Try:

Code: [Select]
StrToIntDef(Edit1.Text, 0);
No need to handle exceptions.
« Last Edit: December 16, 2010, 05:44:26 pm by typo »

khf

  • New member
  • *
  • Posts: 10
Re: StrToInt: Problems handling exceptions.
« Reply #2 on: December 16, 2010, 05:55:00 pm »
Very cool! it worked out!

But could you explain to me a little bit how this function works?
I mean: why

StrToIntDef (...)

what does it means "Def" ? and the zero after the argument, why?

And also.... is this the only way? it's the easiest, but what if i want a pop up message who says "Warning, the input must be a number, not a letter! (or an empty case)". In this case, the function you suggested would not be usefull.

Thanks for your help!

typo

  • Hero Member
  • *****
  • Posts: 1368
Re: StrToInt: Problems handling exceptions.
« Reply #3 on: December 16, 2010, 06:05:47 pm »
About the function:
http://www.freepascal.org/docs-html/rtl/sysutils/strtointdef.html

About handling user error:
Code: [Select]
var
  aux :integer;
begin
  aux := StrToIntdef(Edit1.Text, 0);
  if aux = 0 then
    ShowMessage('Invalid integer value');
  scrollbar1.Position := aux;
end;
« Last Edit: December 16, 2010, 06:07:31 pm by typo »

eny

  • Hero Member
  • *****
  • Posts: 812
Re: StrToInt: Problems handling exceptions.
« Reply #4 on: December 16, 2010, 06:08:52 pm »
Very cool! it worked out!

But could you explain to me a little bit how this function works?

StrToIntDef in FPC docs...

(Hm, the documentation in the above link contains a copy/paste error in the example code...)
« Last Edit: December 16, 2010, 06:10:30 pm by eny »
WinXP Prof SP3; Lazarus 0.9.30; FPC 2.4.4; 2011-06-02 (#29749)

khf

  • New member
  • *
  • Posts: 10
Re: StrToInt: Problems handling exceptions.
« Reply #5 on: December 16, 2010, 06:39:06 pm »
Nice!
Finally, i decided to put

Code: [Select]
aux:=StrToIntDef(edit1.text, -1);
if aux=-1 then ShowMessage('!!!')
else scrollbar.position:=aux;

Because 0 is a valid number for the scroll bar.

But if i need the number zero and also negative and positive numbers..... is there any other way for handling user errors?

typo

  • Hero Member
  • *****
  • Posts: 1368
Re: StrToInt: Problems handling exceptions.
« Reply #6 on: December 16, 2010, 06:41:41 pm »
You need to set a default integer value to return. It can be, for example, -MaxInt, which will give you a reasonable range of valid values.
« Last Edit: December 16, 2010, 06:48:29 pm by typo »

khf

  • New member
  • *
  • Posts: 10
Re: StrToInt: Problems handling exceptions.
« Reply #7 on: December 16, 2010, 06:55:49 pm »
Understood! Thanks for your explanation!

Problem Solved!

OpenLieroXor

  • New member
  • *
  • Posts: 38
Re: [SOLVED] StrToInt: Problems handling exceptions.
« Reply #8 on: December 16, 2010, 07:37:21 pm »
IMHO better solution for that is handling the exception during conversion. The code you posted in the first post (try...except construction) is correct, however lazarus debugger will catch the exception and display a message box, even if the exception is handled. It won't happen while running application outside the IDE. To avoid displaying the pop-up during debugging, you need to switch off catching the EConvertError exception in debugger's options.

sfeinst

  • Jr. Member
  • **
  • Posts: 99
Re: [SOLVED] StrToInt: Problems handling exceptions.
« Reply #9 on: December 16, 2010, 09:44:51 pm »
Another option if you want to allow any value but want to check if the user had an error is to use TryStrToInt.

var
  aux: integer;
begin
  if not TryStrToInt(Edit1.Text, aux) then
     ShowMessage('User error')
  else
     ...continue on with your processing

khf

  • New member
  • *
  • Posts: 10
Re: [SOLVED] StrToInt: Problems handling exceptions.
« Reply #10 on: December 19, 2010, 12:41:44 am »
Very interesting guys! I will try this new solution and switch off the debugger's option.

Sorry by the late reply, but I posted this topic in the incorrect section! That's the reason why i could not enter to this topic before (i could not found it)

Cheers!!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads