?
Solved

User-Defined Exception Raising From Within DLLs

Posted on 1997-03-21
5
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, left to enroll

770 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