Autor Beitrag
-:SID:-
Hält's aus hier
Beiträge: 3

WIN 2000, WIN XP
Delphi 7
BeitragVerfasst: Mi 25.06.08 16:16 
Laut Definition (Delphi 7) soll der Aufruf von exit innerhalb eines try..finally- Blocks nicht zum Ende der Methode springen, sondern auf den ersten Befehl im finally..end- Block.

Ich habe jetzt das Problem, daß exit immer zum Ende springt, folgendes Konstrukt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure Meine.Methode;
var
  EnableMainForm : boolean;
  ...
begin
  try
    ...
    if assigned (MainCmdList) and (MainCmdList.Count < 2then begin
      EnableMainForm := true;
      exit;   // <-- hier springt er direkt zum Ende der Methode und nicht in den finally- Block
    end;
    ... weiterer Code ...
  finally
    if (not Application.ShowMainForm) and EnableMainForm then begin
      ... wichtiger Code, wird übersprungen ...
    end;
    ...
  end;   //finally
end;   //Meine.Methode


Es liegt nicht an der Optimierung, die ist aus.
Hatte jemand schon mal dieses Problem ?
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: Mi 25.06.08 16:25 
den weiterencode nach dem if könntest du auch in einen else block schreiben, dann hättest du das problem nich
-:SID:- Threadstarter
Hält's aus hier
Beiträge: 3

WIN 2000, WIN XP
Delphi 7
BeitragVerfasst: Mi 25.06.08 16:37 
Hi ! Danke für die schnelle Antwort :D
Das wäre eine Möglichkeit, allerdings hätte ich dann sehr viele verschachtelte else- Zweige, da nach diesem Code- Fragment noch einige solche Konstrukte kommen (siehe ...weiterer Code...). Die Verwendung von exit sollte die Code- Übersicht erhöhen.
Das generelle Problem ist, daß man sich anscheinend nicht darauf verlassen kann, daß exit immer in den finally- Block seiner Ebene springt.
Gibt es vielleicht eine Compiler- Einstellung, etc., die das Problem verursacht ?
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Mi 25.06.08 16:47 
Hallo,

mir ist das auch schon aufgefallen. Allerdings hatte ich im letzen Projekt einen großen Bug, bis ich merkte, dass finally trotz Exit ausgeführt wurde. Früher war es garantiert nicht so - ich habe keine Ahnung, was da der Unterschied war.

Grüße,
Yogu
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 25.06.08 17:21 
Moin und :welcome: im Forum!

user profile icon-:SID:- hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure Meine.Methode;
var
  EnableMainForm : boolean;
  ...
begin
  try
    ...
    if assigned (MainCmdList) and (MainCmdList.Count < 2then begin
      EnableMainForm := true;
      exit;   // <-- hier springt er direkt zum Ende der Methode und nicht in den finally- Block
    end;
    ... weiterer Code ...
  finally
    ShowMessage('finally erreicht!');
    if (not Application.ShowMainForm) and EnableMainForm then begin
      ... wichtiger Code, wird übersprungen ...
    end;
    ...
  end;   //finally
end;   //Meine.Methode
Probier´s doch einfach mal aus. :idea: ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Mi 25.06.08 17:28 
Hallo,

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
try
  Exit;
finally
  ShowMessage('Finally erreicht');
end;

Verläuft bei mir positiv (Die Meldung erscheint).

Grüße,
Yogu
IceBube
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 231



BeitragVerfasst: Mi 25.06.08 17:39 
Könntest ja auch so machen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
procedure Meine.Methode; 
var 
  EnableMainForm : boolean; 
  ... 
begin 
  try 
    ... 
    if assigned (MainCmdList) and (MainCmdList.Count < 2then begin 
      EnableMainForm := true; 
      //Hier einfach erneut den Code der übersprungen wird ausführen...
       ... wichtiger Code, wird übersprungen ... 

      exit;   // <-- hier springt er direkt zum Ende der Methode und nicht in den finally- Block 
    end
    ... weiterer Code ... 
  finally 
    if (not Application.ShowMainForm) and EnableMainForm then begin 
      ... wichtiger Code, wird übersprungen ... 
    end
    ... 
  end;   //finally 
end;   //Meine.Methode


Edit:
Wird wahrscheinlich an dem liegen?!

ausblenden Delphi-Quelltext
1:
if (not Application.ShowMainForm) and EnableMainForm then begin					


Trotzdem komisch das die Methode nicht ausgeführt wird...

lg
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 25.06.08 20:15 
Setz da mal nen Haltepunkt, und schau dir an, wie die Variablen stehen..
-:SID:- Threadstarter
Hält's aus hier
Beiträge: 3

WIN 2000, WIN XP
Delphi 7
BeitragVerfasst: Do 26.06.08 08:51 
Also der Haltepunkt im finally- Block wurde nie erreicht, beim Steppen war ich nach Exit bei:
end; //Meine.Methode

Ich habe das Problem jetzt durch Umstrukturieren gelöst, jetzt funktioniert es.
Vielen Dank für die Tips.