AggregateException Klasse
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Stellt einen oder mehrere Fehler dar, die während der Anwendungsausführung auftreten.
public ref class AggregateException : Exception
public class AggregateException : Exception
[System.Serializable]
public class AggregateException : Exception
type AggregateException = class
inherit Exception
[<System.Serializable>]
type AggregateException = class
inherit Exception
Public Class AggregateException
Inherits Exception
- Vererbung
- Attribute
Beispiele
Im folgenden Beispiel wird die AggregateException Ausnahme abgefangen und die Methode aufgerufen, um jede Handle darin enthaltene Ausnahme zu behandeln. Das Kompilieren und Ausführen des Beispiels mit der ersten task1 Variablen sollte zu einem AggregateException Objekt führen, das eine UnauthorizedAccessException Ausnahme enthält. Wenn Sie diese Zeile kommentieren, die Zweite task1 Variable auskommentieren und das Kompilieren und Ausführen des Beispiels ausführen, wird ein AggregateException Objekt erstellt, das eine IndexOutOfRangeException Ausnahme enthält.
using System;
using System.IO;
using System.Threading.Tasks;
class Example
{
static async Task Main(string[] args)
{
// Get a folder path whose directories should throw an UnauthorizedAccessException.
string path = Directory.GetParent(
Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile)).FullName;
// Use this line to throw UnauthorizedAccessException, which we handle.
Task<string[]> task1 = Task<string[]>.Factory.StartNew(() => GetAllFiles(path));
// Use this line to throw an exception that is not handled.
// Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); } );
try
{
await task1;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Caught unauthorized access exception-await behavior");
}
catch (AggregateException ae)
{
Console.WriteLine("Caught aggregate exception-Task.Wait behavior");
ae.Handle((x) =>
{
if (x is UnauthorizedAccessException) // This we know how to handle.
{
Console.WriteLine("You do not have permission to access all folders in this path.");
Console.WriteLine("See your network administrator or try another path.");
return true;
}
return false; // Let anything else stop the application.
});
}
Console.WriteLine("task1 Status: {0}{1}", task1.IsCompleted ? "Completed," : "",
task1.Status);
}
static string[] GetAllFiles(string str)
{
// Should throw an UnauthorizedAccessException exception.
return System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories);
}
}
// The example displays the following output if the file access task is run:
// You do not have permission to access all folders in this path.
// See your network administrator or try another path.
// task1 Status: Completed,Faulted
// It displays the following output if the second task is run:
// Unhandled Exception: System.AggregateException: One or more errors occurred. ---
// > System.IndexOutOfRangeException: Index was outside the bounds of the array.
// at Example.<Main>b__0()
// at System.Threading.Tasks.Task.Execute()
// --- End of inner exception stack trace ---
// at System.AggregateException.Handle(Func`2 predicate)
// at Example.Main(String[] args)
open System
open System.IO
open System.Threading.Tasks
let getAllFiles str =
// Should throw an UnauthorizedAccessException exception.
System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories)
// Get a folder path whose directories should throw an UnauthorizedAccessException.
let path =
let directory =
Environment.SpecialFolder.UserProfile
|> Environment.GetFolderPath
|> Directory.GetParent
directory.FullName
// Use this line to throw an exception that is not handled.
// let task1 = Task<string []>.Factory.StartNew(fun () -> raise (IndexOutOfRangeException()) )
let task1 = Task.Factory.StartNew(fun () -> getAllFiles (path))
let execute () =
try
task1.Wait()
with
| :? UnauthorizedAccessException -> printfn "Caught unauthorized access exception-await behavior"
| :? AggregateException as ae ->
printfn "Caught aggregate exception-Task.Wait behavior"
ae.Handle (fun x ->
match x with
| :? UnauthorizedAccessException ->
printfn "You do not have permission to access all folders in this path."
printfn "See your network administrator or try another path."
true
| _ -> false)
printfn $"""task1 Status: {if task1.IsCompleted then "Completed," else ""}{task1.Status}"""
execute ()
// The example displays the following output if the file access task is run:
// You do not have permission to access all folders in this path.
// See your network administrator or try another path.
// task1 Status: Completed,Faulted
// It displays the following output if the second task is run:
// Unhandled exception. System.AggregateException: One or more errors occurred. (Index was outside the bounds of the array.) (Index was outside the bounds of the array.)
// ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
// at Exception1.task1@19.Invoke()
// at System.Threading.Tasks.Task`1.InnerInvoke()
// at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
// at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
// --- End of stack trace from previous location ---
// at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
// at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
// --- End of inner exception stack trace ---
// at System.AggregateException.Handle(Func`2 predicate)
// at <StartupCode$exception1>.$Exception1.main@()
Imports System.IO
Imports System.Threading.Tasks
Module Example
Sub Main()
' Get a folder path whose directories should throw an UnauthorizedAccessException.
Dim path As String = Directory.GetParent(
Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile)).FullName
' Use this line to throw UnauthorizedAccessException, which we handle.
Dim task1 = Task(Of String()).Factory.StartNew(Function() GetAllFiles(path))
' Use this line to throw an exception that is not handled.
' Task task1 = Task.Factory.StartNew(Sub() Throw New IndexOutOfRangeException() )
Try
task1.Wait()
Catch ae As AggregateException
ae.Handle(Function(x)
If TypeOf (x) Is UnauthorizedAccessException Then ' This we know how to handle
Console.WriteLine("You do not have permission to access all folders in this path.")
Console.WriteLine("See your network administrator or try another path.")
Return True
Else
Return False ' Let anything else stop the application.
End If
End Function)
End Try
Console.WriteLine("task1 Status: {0}{1}", If(task1.IsCompleted, "Completed,", ""),
task1.Status)
End Sub
Function GetAllFiles(ByVal str As String) As String()
' Should throw an UnauthorizedAccessException exception.
Return System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories)
End Function
End Module
Hinweise
AggregateException wird verwendet, um mehrere Fehler in einem einzelnen, ausgelösten Ausnahmeobjekt zu konsolidieren. Es wird umfassend in der Task Parallel Library (TPL) und Parallel LINQ (PLINQ) verwendet. Weitere Informationen finden Sie unter Exception Handling and How to: Handle Exceptions in a PLINQ Query. Weitere Informationen finden Sie im Blog Aggregating Exceptions im Blog .NET Matters.
Konstruktoren
| Name | Beschreibung |
|---|---|
| AggregateException() |
Initialisiert eine neue Instanz der AggregateException Klasse mit einer vom System bereitgestellten Meldung, die den Fehler beschreibt. |
| AggregateException(Exception[]) |
Initialisiert eine neue Instanz der AggregateException Klasse mit Verweisen auf die inneren Ausnahmen, die die Ursache dieser Ausnahme sind. |
| AggregateException(IEnumerable<Exception>) |
Initialisiert eine neue Instanz der AggregateException Klasse mit Verweisen auf die inneren Ausnahmen, die die Ursache dieser Ausnahme sind. |
| AggregateException(SerializationInfo, StreamingContext) |
Veraltet.
Initialisiert eine neue Instanz der AggregateException Klasse mit serialisierten Daten. |
| AggregateException(String, Exception) |
Initialisiert eine neue Instanz der AggregateException Klasse mit einer angegebenen Fehlermeldung und einem Verweis auf die innere Ausnahme, die die Ursache dieser Ausnahme ist. |
| AggregateException(String, Exception[]) |
Initialisiert eine neue Instanz der AggregateException Klasse mit einer angegebenen Fehlermeldung und verweist auf die inneren Ausnahmen, die die Ursache dieser Ausnahme darstellen. |
| AggregateException(String, IEnumerable<Exception>) |
Initialisiert eine neue Instanz der AggregateException Klasse mit einer angegebenen Fehlermeldung und verweist auf die inneren Ausnahmen, die die Ursache dieser Ausnahme darstellen. |
| AggregateException(String) |
Initialisiert eine neue Instanz der AggregateException Klasse mit einer angegebenen Meldung, die den Fehler beschreibt. |
Eigenschaften
| Name | Beschreibung |
|---|---|
| Data |
Ruft eine Auflistung von Schlüssel-Wert-Paaren ab, die zusätzliche benutzerdefinierte Informationen zur Ausnahme bereitstellen. (Geerbt von Exception) |
| HelpLink |
Dient zum Abrufen oder Festlegen eines Links zur Hilfedatei, die dieser Ausnahme zugeordnet ist. (Geerbt von Exception) |
| HResult |
Dient zum Abrufen oder Festlegen von HRESULT, einem codierten numerischen Wert, der einer bestimmten Ausnahme zugewiesen ist. (Geerbt von Exception) |
| InnerException |
Ruft die Exception Instanz ab, die die aktuelle Ausnahme verursacht hat. (Geerbt von Exception) |
| InnerExceptions |
Ruft eine schreibgeschützte Auflistung der Exception Instanzen ab, die die aktuelle Ausnahme verursacht haben. |
| Message |
Ruft eine Nachricht ab, die die Ausnahme beschreibt. |
| Message |
Ruft eine Nachricht ab, die die aktuelle Ausnahme beschreibt. (Geerbt von Exception) |
| Source |
Dient zum Abrufen oder Festlegen des Namens der Anwendung oder des Objekts, das den Fehler verursacht. (Geerbt von Exception) |
| StackTrace |
Ruft eine Zeichenfolgendarstellung der unmittelbaren Frames im Aufrufstapel ab. (Geerbt von Exception) |
| TargetSite |
Ruft die Methode ab, die die aktuelle Ausnahme auslöst. (Geerbt von Exception) |
Methoden
| Name | Beschreibung |
|---|---|
| Equals(Object) |
Bestimmt, ob das angegebene Objekt dem aktuellen Objekt entspricht. (Geerbt von Object) |
| Flatten() |
Vereinfacht eine AggregateException Instanz in eine einzelne, neue Instanz. |
| GetBaseException() |
Gibt die Exception Ursache dieser Ausnahme zurück. Diese Ausnahme ist entweder die Stamm exception oder die erste AggregateException , die entweder mehrere innere Ausnahmen oder gar keine inneren Ausnahmen enthält. |
| GetHashCode() |
Dient als Standardhashfunktion. (Geerbt von Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Veraltet.
Initialisiert eine neue Instanz der AggregateException Klasse mit serialisierten Daten. |
| GetType() |
Ruft den Laufzeittyp der aktuellen Instanz ab. (Geerbt von Exception) |
| Handle(Func<Exception,Boolean>) |
Ruft einen Handler für jedes Exception enthaltene Ereignis AggregateExceptionauf. |
| MemberwiseClone() |
Erstellt eine flache Kopie der aktuellen Object. (Geerbt von Object) |
| ToString() |
Erstellt und gibt eine Zeichenfolgendarstellung der aktuellen AggregateExceptionzurück. |
Ereignisse
| Name | Beschreibung |
|---|---|
| SerializeObjectState |
Veraltet.
Tritt auf, wenn eine Ausnahme serialisiert wird, um ein Ausnahmestatusobjekt zu erstellen, das serialisierte Daten zu der Ausnahme enthält. (Geerbt von Exception) |
Gilt für:
Threadsicherheit
Alle öffentlichen und geschützten Member von AggregateException sind threadsicher und können gleichzeitig aus mehreren Threads verwendet werden.