Solved

User-Defined Exception Raising From Within DLLs

Posted on 1997-03-21
5
250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 165
Working this component WebCopy with proxy, help 3 92
Mobile Keyboard covers the display of the TMemo 3 96
select query - oracle 16 100
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…

860 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