Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#1

EAggregateException

  Alt 1. Mai 2018, 09:18
Da keine Dokumentation zu System.Threading.EAggregateException existiert gehe ich einmal davon aus dass man sich an .NET's System.AggregateException orientiert hat und lese als Ersatz dessen Doku:

Zitat:
AggregateException is used to consolidate multiple failures into a single, throwable exception object. It is used extensively in the Task Parallel Library (TPL) and Parallel LINQ (PLINQ). For additional information, see the Aggregating Exceptions entry in the .NET Matters blog.
Meine Zwei Fragen:
  1. Auch wenn es in System.Threading steckt hat es nicht zwangsläufig mit Threads/Tasks zu tun. Ich "darf" es auch als Ergebnis von normaler, serieller Abarbeitung nehmen, richtig?
  2. Angenommen mehrere Durchgänge einer for-Schleife schlagen fehl und ich möchte das in Form einer EAggregateException nach oben hin kenntlich machen. Ist das im Folgenden so ok?

Delphi-Quellcode:
uses System.Threading;

procedure processData(const input: Integer);
begin
   if Odd(input) then
      raise EProgrammerNotFound.CreateFmt('Error while processing "%d"', [input]);
end;

procedure doStuff();
var
   exceptions: TArray<Exception>;
   i: Integer;
begin
   exceptions := [];
   for i := 0 to 5 do
      try
         processData(i);
      except
         exceptions := exceptions + [Exception(AcquireExceptionObject())];
         Continue;
      end;

   case Length(exceptions) of
      0: Exit;
      1: raise exceptions[0];
   else
      raise EAggregateException.Create('Calculations failed', exceptions);
   end;
end;

Geändert von Der schöne Günther ( 1. Mai 2018 um 09:24 Uhr)
  Mit Zitat antworten Zitat