Solved

Trapping errors in try .. except

Posted on 2002-04-03
2
184 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now