Solved

User-Defined Exception Raising From Within DLLs

Posted on 1997-03-21
5
253 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
ID: 1334814
The C++ code I was asking for is just for the app part, not the DLL part.
0
 
LVL 3

Expert Comment

by:sperling
ID: 1334815
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
ID: 1334816

0
 

Author Comment

by:comptec94031
ID: 1334817
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
ID: 1334818
[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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: ForceDirectory plain function 7 48
Microsoft Access 97 and Delphi XE2 9 71
Delphi, TImage, Png 2 18
TlistView is Really heavy on Android 3 11
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…

685 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