Solved

Trapping errors in try .. except

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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