A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

TryStrToInt


TryStrToInt() is a delphi and fpc function that allows you to convert a string to integer, and return an error if it fails. People use TryStrToInt instead of StrToInt, because StrToInt makes you use ugly expensive exceptions.
function TryStrToInt(const s: string; out i: Integer):Boolean;
(returns a boolean... as error)
var
  number: integer;
  success: boolean;
begin
  success = TryStrToInt('12345', number);
  if not success then 
    writeln('there was a problem converting a string to integer');
end.

Delphi programmers (and fpc) argue that exceptions are more elegant than error code systems in plain C code.. but hypocritically, delphi programmers use error codes to avoid using exceptions, in cases such as TryStrToInt, to work around their very exception system that they think so highly of. This is an on going flamewar, and some people arrogantly think they have solved all the problems of errors by saying Out of Site, Out of Mind and pretending we can wish away all our problems with exceptions.

More is discussed on:

Improve-or-Ditch-The-Rude-Exceptions

Suggestion-for-Modern-Pascal-Error-Contracts

Try-Finally-Is-a-GOTO-That-Is-Too-Limited

About
This site is about programming and other things.
_ _ _