Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Handle OleExceptionsFormat this article printer-friendly!Bookmark function is only available for registered users!
get more Info out of a EOleException
Product:
Delphi 5.x (or higher)
Category:
ActiveX
Skill Level:
Scoring:
Last Update:
07/10/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet EOleException MessageDlgPosHelp ShowException HelpContext HelpFile
Times Scored:
8
Visits:
4348
Uploader: Andreas Schmidt
Company:
Reference: N/A
 
Question/Problem/Abstract:
a EOleException has more information than
the default handler shows.
Answer:



Usually the default Exception handler shows only the Message of
a exception.

But a EOleException provides more information.

    property ErrorCode: HRESULT;
    property HelpFile: string;
    property Source: string;

So we have to write our own exception handler.
I suggest to implement the Exception handler on the main form.


TMainForm = class(TForm)

private

    procedure InstallExceptionHandler;
    procedure ExceptionHandler(Sender: TObject; E: Exception);
public

end;



implementation

Uses ....,comobj;


// call the following procedure from OnCreate on the form
procedure TMainFrm.InstallExceptionHandler;
begin
   Application.OnException := ExceptionHandler;
end;



function HResultToStr(hr : HRESULT):string;
type
  
TFLookup = record
      
fc : Integer;
      fs : string;
   end;

const
  
farray : array[0..21] of TFLookup = (
   ( fc:0;   fs:'Null'),
   ( fc:1;   fs:'RPC'),
   ( fc:2;   fs:'Dispatch'),
   ( fc:3;   fs:'Storage'),
   ( fc:4;   fs:'ITF'),
   ( fc:7;   fs:'Win32'),
   ( fc:8;   fs:'Windows'),
   ( fc:9;   fs:'SSPI'),
   ( fc:10;  fs:'Control'),
   ( fc:11;  fs:'Cert'),
   ( fc:12;  fs:'Internet'),
   ( fc:13;  fs:'Media Server'),
   ( fc:14;  fs:'MSMQ'),
   ( fc:15;  fs:'Setup API'),
   ( fc:16;  fs:'SCard'),
   ( fc:17;  fs:'MTS (?)'),
   ( fc:109; fs:'Visual C++'),
   ( fc:119; fs:'VDI (?)'),
   ( fc:769; fs:'IC'),
   ( fc:2047;fs:'Backup'),
   ( fc:2048;fs:'EDB'),
   ( fc:2304;fs:'MDSI')
   );


var
  
i : Integer;
begin
   for
i := low(farray) to high(farray) do
   begin
      if
farray[i].fc = HResultFacility(hr) then
      begin
        
Result := Format('Facility: %s Code: %d', [farray[i].fs, HResultFacility(hr)]);
         Exit;
      end;
   end;

   Result := Format('Facility: %4.4X Code: %d', [HResultFacility(hr), HResultFacility(hr)]);
end;






function FullOleExceptionMessage(E : EOleException):string;
begin
  
// add more information to the exception message
   Result := E.Message+#13#10+
      'Source: '+E.Source+#13#10+
      Format('Error Code: %d [%.8X]', [E.ErrorCode, E.ErrorCode])+#13#10+

  
// the E.ErrorCode is a HRESULT
   // we show this as error type, facility and code
   // severity is allways 1

      HResultToStr(E.ErrorCode)
      ;
end;






procedure ShowOleException(E : EOleException);
var
  
buttons : TMsgDlgButtons;
begin
   if
IsConsole then
   begin
      
Writeln(FullOleExceptionMessage(E));
      if E.Helpfile <> '' then
        
Writeln('see help file ', E.helpfile, 'context: ', E.HelpContext);
   end
   else
   begin
      
buttons := [mbOK];  // OK button for the dialog box
      if E.HelpFile <> '' then
      begin
        
// add a Help button only if a help file was given
         Include(buttons, mbHelp);
      end;
      
// display the exception message
      MessageDlgPosHelp(FullOleExceptionMessage(E), mtError, buttons, E.HelpContext, -1, -1, E.HelpFile);
   end;
end;








// this is our new exception handler
procedure TMainForm.ExceptionHandler(Sender: TObject; E: Exception);
begin
   if
E is EOleException then
      
ShowOleException(EOleException(E))
   else
      
// show other exceptions
      
Application.ShowException(E);
end;








Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)