Error Handling in Delphi

Posted by Edgar Sanz

I am new to delphi and have been using VB for quite a while. Please show me the equivalent of " ON ERROR..." statements of VB in Delphi.


Related Articles and Replies

Re: webmaster Guido :

Given a TButton and a TEdit, you can write the following code:

procedure WhatEverProcedure;
var 
  M, N: integer;
begin
  try
    M := StrToInt(Edit1.Text);
    N := M * 2;
    Label1.Caption := 'times 2 equals ' + IntToStr(N);
  except
    ShowMessage('Not a valid integer number');
  end;
end;

If anything goes wrong during the conversion from the TEdit's text to an integer number, the "try" part is broken off and the program continues immediately after "except". To capture only certain errors, you can specify what errors to look for, like this:

try
  M := StrToInt(Edit1.Text);
  // and so on...
except
  on EConvertError do
  ShowMessage('Not a valid integer number');
end;

All the error types, such as EConvertError, EDivByZero and so on, can be found in Delphi's documentation (help-files and manuals). Look for "exception" and "handling exceptions".