Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

ExceptionExtensions

Mark Smith edited this page Aug 29, 2016 · 1 revision

ExceptionExtensions

A set of extension methods for Exception.

Methods

  • Flatten : walks the InnerException collection and flattens all located exceptions into a single string with CR/LF separators for each Message. Can optionally include the stack trace (by default this is turned off).

Example

try
{
    Task t1 = Task.Run (() => { throw new ArgumentException ("t1"); });
    Task t2 = Task.Run (() => { throw new ArgumentException ("t2"); });
    Task t3 = Task.Run (() => { throw new ArgumentException ("t3"); });

    Task.WaitAll (t1, t2, t3);
}
catch (Exception ex)
{
    var text = ex.Flatten ("Header:", includeStackTrace: true);
    Debug.WriteLine (text);
}

This will output:

Header:Aggregate Exception.
0: System.ArgumentException : t1  at Test.TestPage.<TestPage>m__0 () [0x00006] in /Users/mark/Desktop/Test/Test/TestPage.xaml.cs:23 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/9.8.2.22/src/mono/external/referencesource/mscorlib/system/threading/Tasks/Future.cs:680 
  at System.Threading.Tasks.Task.Execute () [0x00016] in /Library/Frameworks/Xamarin.iOS.framework/Versions/9.8.2.22/src/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502 
1: System.ArgumentException : t2  at Test.TestPage.<TestPage>m__1 () [0x00006] in /Users/mark/Desktop/Test/Test/TestPage.xaml.cs:24 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/9.8.2.22/src/mono/external/referencesource/mscorlib/system/threading/Tasks/Future.cs:680 
  at System.Threading.Tasks.Task.Execute () [0x00016] in /Library/Frameworks/Xamarin.iOS.framework/Versions/9.8.2.22/src/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502 
2: System.ArgumentException : t3  at Test.TestPage.<TestPage>m__2 () [0x00006] in /Users/mark/Desktop/Test/Test/TestPage.xaml.cs:25 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/9.8.2.22/src/mono/external/referencesource/mscorlib/system/threading/Tasks/Future.cs:680 
  at System.Threading.Tasks.Task.Execute () [0x00016] in /Library/Frameworks/Xamarin.iOS.framework/Versions/9.8.2.22/src/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502