.Net 4.5 C# using ExceptionDispatchInfo.Capture to rethrow exceptions

ExceptionDispatchInfo can rethrow an exception from this point where it was captured to the point where it is rethrown. Active exceptions will retain stack trace information. More information can be found at ExceptionDispatchInfo.Capture Method on MSDN.

using System;
using System.Collections.Concurrent;
using System.Runtime.ExceptionServices;
using System.Threading;

namespace CsharpExceptionDemo
{
    class CsharpExceptionExample
    {
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            var exceptions = new BlockingCollection<ExceptionDispatchInfo>();

            ThreadPool.QueueUserWorkItem(_ =>
            {
                try
                {
                    ThrowOne();
                }
                catch (Exception ex)
                {
                    var exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
                
                    exceptions.Add(exceptionDispatchInfo);
                }
                exceptions.CompleteAdding();
            });

            foreach (var exceptionDispatchInfo in exceptions.GetConsumingEnumerable())
            {
                try
                {
                  exceptionDispatchInfo.Throw();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0}", ex);
                }
            }

            Console.ReadKey();
        }

        /// <summary>
        /// Throws Not Supported Exception.
        /// </summary>
        /// <exception cref="System.NotSupportedException"></exception>
        private static void ThrowOne()
        {
            Console.WriteLine("Throw Not Supported Exception");
            
            ThrowTwo();
            
            throw new NotSupportedException();
        }

        /// <summary>
        /// Throws Not Implemented Exception.
        /// </summary>
        /// <exception cref="System.NotImplementedException"></exception>
        private static void ThrowTwo()
        {
            Console.WriteLine("Throw Not Implemented Exception");
            
            ThrowThree();
            
            throw new NotImplementedException();
        }

        /// <summary>
        /// Throws Argument Null Exception.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        private static void ThrowThree()
        {
            Console.WriteLine("Throw Argument Null Exception");
            
            throw new ArgumentNullException();
        }
    }
}

Returns:

Throw Not Supported Exception
Throw Not Implemented Exception
Throw Argument Null Exception
System.ArgumentNullException: Value cannot be null.
   at CsharpExceptionDemo.CsharpExceptionExample.ThrowThree() in c:\Users\csharper\
Documents\Visual Studio 2012\Projects\CsharpExceptionDemo\CsharpExceptionDemo\Cs
harpExceptionExample.cs:line 81
   at CsharpExceptionDemo.CsharpExceptionExample.ThrowTwo() in c:\Users\csharper\Do
cuments\Visual Studio 2012\Projects\CsharpExceptionDemo\CsharpExceptionDemo\Csha
rpExceptionExample.cs:line 68
   at CsharpExceptionDemo.CsharpExceptionExample.ThrowOne() in c:\Users\csharper\Do
cuments\Visual Studio 2012\Projects\CsharpExceptionDemo\CsharpExceptionDemo\Csha
rpExceptionExample.cs:line 55
   at CsharpExceptionDemo.CsharpExceptionExample.<>c__DisplayClass1.<Main>b__0(O
bject _) in c:\Users\csharper\Documents\Visual Studio 2012\Projects\CsharpException
Demo\CsharpExceptionDemo\CsharpExceptionExample.cs:line 21
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at CsharpExceptionDemo.CsharpExceptionExample.Main() in c:\Users\csharper\Docume
nts\Visual Studio 2012\Projects\CsharpExceptionDemo\CsharpExceptionDemo\CsharpEx
ceptionExample.cs:line 36

Leave a Reply

Your email address will not be published. Required fields are marked *