I am creating a simple delphi console app which does arithmetic operations of the number entered by user, problem is that the user can enter ‘a’..’z’,’A’..’Z’ which leads to app crash. How can i prevent this? Using try-except does not work, anybody has ideas on preventing this crash?
Here is my code:
Write('Please Enter a Number:=');
Readln(str1);
try
num1:=StrToFloat(str1);
except
on e:exception do
Writeln(e.message);
end;
Thanks in Advance.
As David, points out, exceptions work just as fine in console apps as they do in GUI apps. For instance, this works flawlessly:
An alternative not using exceptions is to use the
TryStrToFloatfunction: