Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.184 Beiträge
 
Delphi 12 Athens
 
#5

Re: Wann werden Exception-Objekte freigegeben?

  Alt 9. Sep 2009, 20:57
Nee, das hat sich über die Jahre erfolgreich vor mir versteckt

Hmmm, diese Art der Verschachtelung kommt bei mir recht selten vor.

Aktuell sieht es bei mir so aus ... bei einem Re-Raise wird die alte Exception der Neuen im Constructor mitgegeben und die Messages werden samt Klassen- und Funktionsnamen miteinander verbunden.
Delphi-Quellcode:
Type EXMLException = Class(Exception)
     _Info: Array of Record
       ErrorClass: TClass;
       FunctionsName: String;
       Message: String;
     End;
     Const MaxXMLErrStr = 200;
     Class Function Str(S: AnsiString; MaxLen: Integer = MaxXMLErrStr): String; Overload;
     Class Function Str(S: UnicodeString; MaxLen: Integer = MaxXMLErrStr): String; Overload;
     Constructor Create(ErrorClass: TClass; Const FunctionsName: String;
       ResStringRec: PResStringRec; Const Args: Array of Const;
       PrevException: Exception = nil; ErrorCode: LongWord = ERROR_SUCCESS); Overload;
     Constructor Create(ErrorClass: TClass; Const FunctionsName: String;
       ResStringRec: PResStringRec; Const S: UnicodeString); Overload;
     Constructor Create(ErrorClass: TClass; Const FunctionsName: String;
       ResStringRec: PResStringRec; i: Integer = 0); Overload;
  End;

Class Function EXMLException.Str(S: AnsiString; MaxLen: Integer = MaxXMLErrStr): String;
  Const Width: Array[Boolean] of Byte = (2, 4);

  Var i: Integer;

  Begin
    If MaxLen < 0 Then Begin
      If Length(S) > -MaxLen Then S := Copy(S, 1, -MaxLen);
    End Else
      If Length(S) > MaxLen Then S := Copy(S, 1, MaxLen) + '...';
    For i := Length(S) downto 1 do
      If Ord(S[i]) in [0{..9, 11, 12, 14.}..31, 34, 127, 160, 255] Then Begin
        Insert(AnsiString(Format('%.*x', [Width[(i < Length(S))
          and (Ord(S[i + 1]) in [Ord('0')..Ord('9'), Ord('A')..Ord('F'),
            Ord('a')..Ord('f')])], Ord(S[i])])), S, i + 1);
        S[i] := '#';
      End;
    Result := String(S);
  End;

Class Function EXMLException.Str(S: UnicodeString; MaxLen: Integer = MaxXMLErrStr): String;
  Const Width: Array[Boolean] of Byte = (2, 4);

  Var i: Integer;

  Begin
    If MaxLen < 0 Then Begin
      If Length(S) > -MaxLen Then S := Copy(S, 1, -MaxLen);
    End Else
      If Length(S) > MaxLen Then S := Copy(S, 1, MaxLen) + '...';
    For i := Length(S) downto 1 do
      If ((SizeOf(Char) = 1) and (Ord(S[i]) > 255))
          or (Ord(S[i]) in [0{..9, 11, 12, 14.}..31, 34, 127, 160, 255]) Then Begin
        Insert(UnicodeString(Format('%.*x', [Width[(i < Length(S))
          and (Ord(S[i + 1]) in [Ord('0')..Ord('9'), Ord('A')..Ord('F'),
            Ord('a')..Ord('f')])], Ord(S[i])])), S, i + 1);
        S[i] := '#';
      End;
    Result := String(S);
  End;

Constructor EXMLException.Create(ErrorClass: TClass; Const FunctionsName: String; ResStringRec: PResStringRec;
    Const Args: Array of Const; PrevException: Exception = nil; ErrorCode: LongWord = ERROR_SUCCESS);

  Var i: Integer;

  Begin
    If PrevException is EXMLException Then
      _Info := Copy(EXMLException(PrevException)._Info)
    Else If PrevException is Exception Then Begin
      SetLength(_Info, 1);
      _Info[0].ErrorClass := ErrorClass;
      _Info[0].FunctionsName := '-';
      _Info[0].Message := Exception(ErrorClass).Message;
    End;
    If ErrorCode <> ERROR_SUCCESS Then Begin
      i := Length(_Info);
      SetLength(_Info, i + 1);
      _Info[i].ErrorClass := nil;
      _Info[i].FunctionsName := 'System';
      _Info[i].Message := SysErrorMessage(ErrorCode);
    End;
    i := Length(_Info);
    SetLength(_Info, i + 1);
    _Info[i].ErrorClass := ErrorClass;
    _Info[i].FunctionsName := FunctionsName;
    _Info[i].Message := Format(LoadResString(ResStringRec), Args);
    Message := Format('[%s] ' + '%s.%s:' + sLineBreak + '%s', [ClassName,
      _Info[i].ErrorClass.ClassName, _Info[i].FunctionsName, _Info[i].Message]);
    While (i > 0) and (Length(Message) < 3000) do Begin
      Dec(i);
      Message := Format('%s' + sLineBreak + sLineBreak + '%s.%s:' + sLineBreak + '%s', [Message,
        _Info[i].ErrorClass.ClassName, _Info[i].FunctionsName, _Info[i].Message]);
    End;
    If Length(Message) > 3500 Then Message := Copy(Message, 1, 3497) + '...';
  End;

Constructor EXMLException.Create(ErrorClass: TClass; Const FunctionsName: String;
    ResStringRec: PResStringRec; Const S: UnicodeString);
  Begin
    Create(ErrorClass, FunctionsName, ResStringRec, [Str(S)]);
  End;

Constructor EXMLException.Create(ErrorClass: TClass; Const FunctionsName: String;
    ResStringRec: PResStringRec; i: Integer = 0);
  Begin
    Create(ErrorClass, FunctionsName, ResStringRec, [i]);
  End;
und aufrufen tu ich's dann etwa so
Delphi-Quellcode:
Procedure TXMLFile.SetLineFeed(Const Value: UnicodeString);
  Begin
    If ... Then
      Raise EXMLException.Create(Self.ClassType, 'LineFeed', @SInvalidValue, Value);
    _LineFeed := Value;
  End;
$2B or not $2B
  Mit Zitat antworten Zitat