Solved

Trapping errors in try .. except

Posted on 2002-04-03
2
187 Views
Last Modified: 2010-04-04
Can I know the codes to trapping error returned and displaying appropriate messages?

I have this code :
  try
  if DataSet.Modified then
  begin
    if MessageDlg('Do you want to save the record', mtConfirmation, [mbYes, mbNo],0) = mrNo then
    begin
       DataSet.Cancel;
       SysUtils.Abort;
    end
  end;

  except
    ShowMessage ('Record already exist. (Datamodule) Please enter a different date.');
    DataSet.Cancel;
    SysUtils.Abort;
  end;
end;

Currently, if my answer is no, it will create a silent exception and the msg will be shown. I only want that message to be shown if the record already exist in the database. How can i check the error code returned and react accordingly? Thanks!
0
Comment
Question by:irene79
2 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6916910
in the except do

except
  on E: EUpdateError do begin
    case E.ErrorCode of
      xx:
    end;
  end;

from the help file : Note:     If ErrorCode is 0, use the OriginalException property, which provides access to the exception that was caught.

0
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
ID: 6917315
You could check for a Yes in stead of a No:

 if DataSet.Modified then
 begin
   if MessageDlg('Do you want to save the record', mtConfirmation, [mbYes, mbNo],0) = mrYes then
  try
    .... post the record ...
  except
    ShowMessage ('Record already exist. (Datamodule) Please enter a different date.');
    DataSet.Cancel;
    SysUtils.Abort;
  end;
 end;

Or you could move the try down a bit:

 if DataSet.Modified then
 begin
   if MessageDlg('Do you want to save the record', mtConfirmation, [mbYes, mbNo],0) = mrNo then
   begin
      DataSet.Cancel;
      SysUtils.Abort;
   end
 end;
 try

 except
   ShowMessage ('Record already exist. (Datamodule) Please enter a different date.');
   DataSet.Cancel;
   SysUtils.Abort;
 end;

Regards Jacco
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question