[fpc-pascal] A simple "exception handling" question

Bob Richards bob at tamara-b.org
Sat Jan 28 21:45:19 CET 2006


I must not understand a fundamental concept here. Perhaps someone can point me in the
right direction.


{$mode Delphi} //Delphi or objfpc same results


program hextest; // or why can't I trap an exception?


 uses Sysutils,strutils;


 var
  b:byte;
  hex:string;


begin
  Write('Enter a hex String: ');
  ReadLn(hex);
   Try
     b:=Hex2Dec(Hex);
   Except
    On EConvertError do
    Begin
      WriteLn(hex,' is not a valid hex number! Terminating...');
      Halt
    end
   end;
  WriteLn('Hex value ',hex,' is decimal value: ',b)
end.

Upon execution:

$ ./hextest
Enter a hex String: 0A
Hex value 0A is decimal value: 10

Cool!

$ ./hextest
Enter a hex String: FF
Hex value FF is decimal value: 255

Still Cool!

$ ./hextest
Enter a hex String: ZZ
Hex value ZZ is decimal value: 0

Certainly NOT cool!

According to the docs on function "Hex2Dec" in Strutils:

"If S does not contain a valid hexadecimal value, an EConvertError exception will be raised."

So, what profound lack of understanding am I suffering from? Anybody?

Bob





More information about the fpc-pascal mailing list