Solved

Trapping errors in try .. except

Posted on 2002-04-03
2
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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