Try (keyword)

From RemObjects Software

Jump to: navigation, search

This is a Oxygene Keyword topic
Feel free to add your notes to this topic below.



The try statement allows you to write code that 'cleans up', even after errors and it has three forms:

  • try/finally
  • try/except
  • try/except/finally

Use the first version to ensure that resource leaks do not occur and to ensure values are always set even after an error:

method TTestForm.OnClick(Sender: Object; ea: EventArgs);
begin
  try
    fClickCount := fClickCount+1;
    //fNull.ToString();
    //Divide(0);
  finally
    fEdit.Text := fClickCount.ToString;
  end;
end;

With the second version, Exceptions may be caught using a try/except block:

method TTestForm.OnClick(Sender: Object; ea: EventArgs);
begin
  try
    fClickCount := fClickCount+1;
    //fNull.ToString();
    //Divide(0);
  except
    on E:Exception do begin
      MessageBox.Show('Found an error: '+E.Message);
    end;
  end;
end;

Finally, Oxygene allows you to combine these two blocks, e.g:

method TTestForm.OnClick(Sender: Object; ea: EventArgs);
begin
  try
    fClickCount := fClickCount+1;
    //fNull.ToString();
    //Divide(0);
  except
    on E:Exception do begin
      MessageBox.Show('Found an error: '+E.Message);
    end;
  finally
    fEdit.Text := fClickCount.ToString;
  end;
end;


See Also


Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0 Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)

GlossaryKeywordsLanguage FeaturesPlatform FeaturesSamplesArticlesHow ToIssues

Personal tools