Tuesday, 9 March 2010

Delphi - Interbase Tutorial # 4 : Exception Handler

In the previous tutorial, an error occur when you try to delete a record in ‘employee’ table. It is because of the ‘salary’ table refers to the record. An error message will appear. To avoid this, we’re going to create an exception handle. 

Let’s see what kind of error will happen if we try to delete a record in ‘employee’ table. Run the program from Delphi IDE (not the executable file). You’ll see the error message below :

From the message, the exception that raised is EDBEngineError. Let’s see our code for OnClick event of Delete button. You have to add an exception handle code using TRY…..EXCEPT :

procedure TForm1.DeleteClick(Sender: TObject);
begin
 try
  Table1.Delete;
 except
  on EDBEngineError do
  begin
  MessageDlg('You can not delete this record because another table is currently using the data',
  mtError,[mbOK],0)
  end;
 end;
end;

Then if you run the program and try to delete a record in ‘employee’ table that a record in ‘salary’ table refers to it, you’ll get a message like picture below :

Go to Previous Tutorial or Next Tutorial

2 comments:

  1. Thanks for sharing about Delphi . its a very good thing about share knowledge. outsourcing services on Delphi

    ReplyDelete
  2. supplier of 3D printing, rapid prototyping, rapid manufacturing, plastic fabrication, tooling, CNC and injection molding services.

    ReplyDelete

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.