Thema: PersoGen

Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#26

Re: PersoGen

  Alt 14. Sep 2005, 18:46
Dein Exception-Handling ist unsauber/fehlerhaft!
Delphi-Quellcode:
function IsValidID(ID: string): boolean;
begin
  try
    Row1 := .....
    ..... // ***
    if not
        ((Ps1 = StrToInt(ID[10])) and
        (Ps2 = StrToInt(ID[20])) and
        (Ps3 = StrToInt(ID[28])) and
        (Ps4 = StrToInt(ID[36]))) then
      result := false
    else
      result := true;
  except
    exit
  end;
end;
Wenn nun an der Stelle, die mit *** gekennzeichnet wurde eine Exception auftritt ??
Du wirst niemals erfahren, was schiefgelaufen ist und ausserdem ein undefiniertes Result erhalten.
Korrektes Exception-Handling sieht so aus:
Delphi-Quellcode:
function IsValidID(ID: string): boolean;
begin
  try
    Row1 := .....
    ..... // ***
    if not
        ((Ps1 = StrToInt(ID[10])) and
        (Ps2 = StrToInt(ID[20])) and
        (Ps3 = StrToInt(ID[28])) and
        (Ps4 = StrToInt(ID[36]))) then
      result := false
    else
      result := true;
  except
  on E:Exception do
  begin
     E.Message := 'Fehler in IsValidID('+ID+')'#13#10+
        E.Message;
     raise;
  end;
  end;
end;
Der Aufrufer, bekommt eine recht genaue Beschreibung, was schiefgelaufen ist.
Die Exception wird nur abgefangen und erneut ausgelöst.
Andreas
  Mit Zitat antworten Zitat