Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Exception in Anwendung ohne Form (https://www.delphipraxis.net/173363-exception-anwendung-ohne-form.html)

blablab 20. Feb 2013 07:43

Exception in Anwendung ohne Form
 
Hallo!

Ich hab ein kleines Programm ohne Forms. Das macht 100ms lang was und schließt sich dann wieder, fertig. Aber falls ein Fehler auftritt würde ich gerne eine Fehlermeldung anzeigen. Wenn ich aber eine Exception auslöse bekomme ich von Windows eine Fehlermeldung "Programm.exe funktioniert nicht mehr" und erst danach wird meine Fehlermeldung gezeigt.
Aber wie kann ich eine eine Exception ohne Windows-Fehlermeldung anzeigen?

Grüße
blablab

DeddyH 20. Feb 2013 07:57

AW: Exception in Anwendung ohne Form
 
Das kann ich nicht nachvollziehen:
Delphi-Quellcode:
program ExceptionTest;

uses
  SysUtils, Windows;

begin
  try
    { TODO -oUser -cConsole Main : Code hier einfügen }
    raise Exception.Create('Alles kaputt hier');
  except
    on E: Exception do
      MessageBox(0, PChar(E.Message), nil, MB_OK or MB_ICONERROR);
  end;
end.

blablab 20. Feb 2013 08:45

AW: Exception in Anwendung ohne Form
 
Gibt es da noch eine andere Möglichkeit? Normalerweise wird ja noch der Exception-Typ und die Adresse angegeben.

Übrigens tritt der Windows-Fehler bei mir nur auf, wenn ich das Programm direkt ausführe, aus Delphi heraus funktioniert es. Ist das etwa nur bei mir der Fall??? (Benutze Delphi7 unter Win7 777)

DeddyH 20. Feb 2013 08:53

AW: Exception in Anwendung ohne Form
 
Die Adresse? Das wäre mir neu, aber Du kannst mein Beispiel ja noch erweitern:
Delphi-Quellcode:
program ExceptionTest;

uses
  SysUtils,
  Windows;

type
  EMyException = class(Exception);

begin
  try
    { TODO -oUser -cConsole Main : Code hier einfügen }
    raise EMyException.Create('Alles kaputt hier');
  except
    on E: Exception do
      MessageBox(0, PChar(Format('%s:%s%s', [E.ClassName, sLineBreak, E.Message])),
        nil, MB_OK or MB_ICONERROR);
  end;
end.

blablab 20. Feb 2013 09:13

AW: Exception in Anwendung ohne Form
 
Danke!

Scheinbar wird es von TApplication auch nicht anders geregelt:
Delphi-Quellcode:
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;

mentaltec 20. Feb 2013 09:28

AW: Exception in Anwendung ohne Form
 
Hi DaddyH,

ich wollt Dir grad um den Hals fallen wegen

Zitat:

Zitat von DeddyH (Beitrag 1204425)
MessageBox(0, PChar(Format('%s:%s%s', [E.ClassName, sLineBreak, E.Message])),

dann hab ich nachgeschaut : sLineBreak iss ja nur CRLF

ich dachte, das wäh'r n Äquivalent zu __LINE__
ich such schon lang nach einem Konstrukt, dass mir die Zeilennummmer des Exceptionwerfers ausgibt

mfg

blablab 20. Feb 2013 09:38

AW: Exception in Anwendung ohne Form
 
Ich habs durch Zufall gefunden! 1:1 dieselbe Meldung die ich nach der Windows-Fehlermeldung bekomme, bekomme ich auch mit:
Delphi-Quellcode:
program ExceptionTest;

uses
   SysUtils,
   Windows;

type
   EMyException = class(Exception);

begin
   try
     { TODO -oUser -cConsole Main : Code hier einfügen }
     raise EMyException.Create('Alles kaputt hier');
   except
     on e: Exception do SysUtils.ShowException(e, nil);
   end;
end.
nur leider weiß ich nicht woher ich die Adresse bekommen soll...

(Ich hab eine eigene Methode mit dem Namen ShowException geschrieben und dann ist mir aufgefallen, dass es das ja schon in SysUtils gibt :-D )


Edit:

Ich habs! Die ultimative Lösung ist :) :
Delphi-Quellcode:
program ExceptionTest;

uses
   SysUtils,
   Windows;

type
   EMyException = class(Exception);

begin
   try
     { TODO -oUser -cConsole Main : Code hier einfügen }
     raise EMyException.Create('Alles kaputt hier');
   except
     on e: Exception do SysUtils.ShowException(e, ExceptAddr);
   end;
end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz