Solved

Trapping errors in try .. except

Posted on 2002-04-03
2
186 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 97
error 1.1 400 Bad request idhttp delphi 18 86
control image tags in a string ? 12 129
Delphi...Split view - idea? 1 67
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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