• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

User-Defined Exception Raising From Within DLLs

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
comptec94031
Asked:
comptec94031
  • 3
1 Solution
 
comptec94031Author Commented:
The C++ code I was asking for is just for the app part, not the DLL part.
0
 
sperlingCommented:
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
 
comptec94031Author Commented:

0
 
comptec94031Author Commented:
Sperling...I accidentally rejected this question...please repaste your answer again and I will finally grade.
0
 
mheacockCommented:
[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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now