![]() |
Exceptions mit Help-Button anzeigen
Beim Erzeugen einer Exception kann ja optional ein Hilfecontext
mitgegeben werden. Beispiel mit Hilfecontext 4570:
Delphi-Quellcode:
Dieses Feature wird aber in der Praxis nicht verwendet,
raise Exception.CreateHelp('Fehler beim Verbuchen.', 4570);
da es beim Anzeigen der Exception keinen Unterschied macht. :( Dies liegt an der schwachen Implementation der Methode ShowException:
Delphi-Quellcode:
Abhilfe schafft folgende Procedure:
// Delphi 5 (evtl. machen's höhere Delphi-Versionen besser)
procedure TApplication.ShowException(E: Exception); var Msg: string; begin Msg := E.Message; if (Msg <> '') and (AnsiLastChar(Msg) > '.') then Msg := Msg + '.'; MessageBox(PChar(Msg), PChar(GetTitle), MB_OK + MB_ICONSTOP); end;
Delphi-Quellcode:
procedure ShowExceptionWithHelp(E:Exception; dlgtype: TMsgDlgType);
var Msg, helpfile: string; buttons : TMsgDlgButtons; begin Msg := E.Message; if (Msg <> '') and (AnsiLastChar(Msg) > '.') then Msg := Msg + '.'; helpfile := Application.HelpFile; if IsConsole then begin Writeln(msg); if (helpfile <> '') and (E.HelpContext <> 0) then Writeln('see help file ', helpfile, 'context: ', E.HelpContext); end else begin buttons := [mbOK]; // OK button for the dialog box if (helpfile <> '') and (E.HelpContext <> 0) then begin // add a Help button only if a help file was given Include(buttons, mbHelp); end; // display the exception message MessageDlgPosHelp(Msg, mtError, buttons, E.HelpContext, -1, -1, helpfile); end; end; Zum Einbinden in eine Anwendung sind noch folgende Schritte notwendig: Event-Handler für Application.OnException erstellen.
Delphi-Quellcode:
Nun muss unser neuer Exception-Handler nur noch dem Application Objekt
procedure TForm1.HandleOnException(Sender: TObject; E: Exception);
begin // hier ist Gelegenheit, Exception in einer Datei zu protokollieren // LogObject.AddMessage(E.Message); ShowExceptionWithHelp(E, mtError); // Aufruf anstelle von Application.ShowException end; bekannt gemacht werden. Dies geschieht im OnCreate des Hauptformulars:
Delphi-Quellcode:
Nur muss nur noch an jeder Stelle der Anwendung, bei der
procedure TForm1.FormCreate(Sender: TObject);
begin Application.OnException := Self.HandleOnException; end; eine Exception ausgelöst wird ein gültiger Hilfekontext bereitgestellt werden und die Hilfedatei mit Erklärungen zu Fehlermeldungen ausgestattet werden. [edit=Matze]Code formatiert. Mfg, Matze[/edit] |
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:50 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz