Solved

User-Defined Exception Raising From Within DLLs

Posted on 1997-03-21
5
237 Views
Last Modified: 2010-04-04
I was have this DLL I wrote a while ago in Delphi 2.  Each function reutrns a possible error code.  I was thinking it might be better to raise an exception and have the calling app catch it.

First, do you think this would be a better way of implementing error handling from within a DLL?

Second, how might I raise my own exceptions from within the DLL?  For example, perhaps I want to raise something like
EKEYWORDNAMENOTFOUND.

Third, could you supply some sample code...just a really small example of the code in the DLL and the Delphi app (perhaps a C++ app too, if you have time, tho not necessary).

Thanks.
0
Comment
Question by:comptec94031
  • 3
5 Comments
 

Author Comment

by:comptec94031
Comment Utility
The C++ code I was asking for is just for the app part, not the DLL part.
0
 
LVL 3

Expert Comment

by:sperling
Comment Utility
Don't use exceptions to return errors from DLL's.

1) A Delphi app won't be able to properly detect the type of an user-defined exception raised in a DLL. This can be done, but it'll be some extra work though. Try raising the silent, EAbort, exception in a DLL. Delphi will think the type is TAbort and show the actual message.

2) Exceptions induces far more overhead than simple error return.  

3) Few of the Windows DLLs uses exceptions for error trapping. Few from 3rd party vendors do. Better stick to the standard.


The "proper" way of doing this would be to wrap the functions available in the DLL in a Delphi or C++ class, and then have the class raise the exceptions based on the return values from the DLL.

To create, raise and catch a delphi exception...

type
  EMyException = class(Exception)
  end;

procedure RaiseTheException;
begin
  .
  .
  raise EMyException.Create('Error');
  .
  .
end;

procedure CatchTheException;
begin
  .
  .
  try
    RaiseTheException;
  except
    on E:EMyException do begin
      MessageDlg('RaiseTheException failed with message :'+E.Message, mtError, [mbOK], 0);
    end;
  end;
  .
  .
end;
0
 

Author Comment

by:comptec94031
Comment Utility

0
 

Author Comment

by:comptec94031
Comment Utility
Sperling...I accidentally rejected this question...please repaste your answer again and I will finally grade.
0
 
LVL 3

Accepted Solution

by:
mheacock earned 75 total points
Comment Utility
[Since your question seems to have disappeared from the list
of questions to be answered...I'll just repaste sperlings question in here]

Don't use exceptions to return errors from DLL's.

                              1) A Delphi app won't be able to properly detect the type of an user-defined exception raised in a DLL. This can be done, but it'll be some extra
                              work though. Try raising the silent, EAbort, exception in a DLL. Delphi will think the type is TAbort and show the actual message.

                              2) Exceptions induces far more overhead than simple error return.

                              3) Few of the Windows DLLs uses exceptions for error trapping. Few from 3rd party vendors do. Better stick to the standard.


                              The "proper" way of doing this would be to wrap the functions available in the DLL in a Delphi or C++ class, and then have the class raise the
                              exceptions based on the return values from the DLL.

                              To create, raise and catch a delphi exception...

                              type
                                EMyException = class(Exception)
                                end;

                              procedure RaiseTheException;
                              begin
                                .
                                .
                                raise EMyException.Create('Error');
                                .
                                .
                              end;

                              procedure CatchTheException;
                              begin
                                .
                                .
                                try
                                  RaiseTheException;
                                except
                                  on E:EMyException do begin
                                  MessageDlg('RaiseTheException failed with message :'+E.Message, mtError, [mbOK], 0);
                                  end;
                                end;
                                .
                                .
                              end;
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

11 Experts available now in Live!

Get 1:1 Help Now