Rethrow() public static method

Rethrows an exception, preserving its stack trace
public static Rethrow ( Exception exception ) : void
exception System.Exception The exception to rethrow
return void
Example #1
0
            public override object WaitForPendingOperationsToComplete(object invocationResult)
            {
                var awaitAdapter = AwaitAdapter.FromAwaitable(invocationResult);

                if (!awaitAdapter.IsCompleted)
                {
                    var waitStrategy = MessagePumpStrategy.FromCurrentSynchronizationContext();
                    waitStrategy.WaitForCompletion(awaitAdapter);
                }

                // Future: instead of Wait(), use GetAwaiter() to check awaiter.IsCompleted above
                // and use awaiter.OnCompleted/awaiter.GetResult below.
                // (Implement a ReflectionAwaitAdapter)
                try
                {
                    invocationResult.GetType().GetMethod(TaskWaitMethod, new Type[0]).Invoke(invocationResult, null);
                }
                catch (TargetInvocationException e)
                {
                    IList <Exception> innerExceptions = GetAllExceptions(e.InnerException);
                    ExceptionHelper.Rethrow(innerExceptions[0]);
                }
                var args = invocationResult.GetType().GetGenericArguments();

                if (args != null && args.Length == 1 && args[0].Name == VoidTaskResultType)
                {
                    return(null);
                }

                PropertyInfo taskResultProperty = invocationResult.GetType().GetProperty(TaskResultProperty, TaskResultPropertyBindingFlags);

                return(taskResultProperty != null?taskResultProperty.GetValue(invocationResult, null) : invocationResult);
            }
 private static object InvokeUnwrapTargetInvocationException(MethodInfo methodInfo, object obj, object[] parameters)
 {
     try
     {
         return(methodInfo.Invoke(obj, parameters));
     }
     catch (TargetInvocationException ex) when(ex.InnerException != null)
     {
         ExceptionHelper.Rethrow(ex.InnerException);
         throw null; // Impossible to get here
     }
 }
Example #3
0
        internal static object?DynamicInvokeWithTransparentExceptions(this Delegate @delegate)
        {
            try
            {
                return(@delegate.DynamicInvoke());
            }
            catch (TargetInvocationException ex)
            {
                ExceptionHelper.Rethrow(ex.InnerException);

                // If this line is reached, ExceptionHelper.Rethrow is very broken.
                throw new InvalidOperationException("ExceptionHelper.Rethrow failed to throw an exception.");
            }
        }
Example #4
0
            public override void BlockUntilCompleted()
            {
                // Normally we would call TaskAwaiter.GetResult (https://source.dot.net/#System.Private.CoreLib/src/System/Runtime/CompilerServices/TaskAwaiter.cs)
                // We will have to polyfill on top of the TPL API.
                // Compare TaskAwaiter.ValidateEnd from Microsoft.Threading.Tasks.dll in Microsoft.Bcl.Async.nupkg.

                try
                {
                    _task.Wait();                                                 // Wait even if the task is completed so that an exception is thrown for cancellation or failure.
                }
                catch (AggregateException ex) when(ex.InnerExceptions.Count == 1) // Task.Wait wraps every exception
                {
                    ExceptionHelper.Rethrow(ex.InnerException);
                }
            }
Example #5
0
        internal static object?InvokeWithTransparentExceptions(this MethodBase methodBase, object?instance)
        {
            // If we ever target .NET Core 2.1, we can keep from mucking with the exception stack trace
            // using BindingFlags.DoNotWrapExceptions rather than try…catch.

            try
            {
                return(methodBase.Invoke(instance, null));
            }
            catch (TargetInvocationException ex)
            {
                ExceptionHelper.Rethrow(ex.InnerException);

                // If this line is reached, ExceptionHelper.Rethrow is very broken.
                throw new InvalidOperationException("ExceptionHelper.Rethrow failed to throw an exception.");
            }
        }
Example #6
0
        public static object Await(Func <object> invoke)
        {
            Guard.ArgumentNotNull(invoke, nameof(invoke));

            object invocationResult;

            using (InitializeExecutionEnvironment())
            {
                invocationResult = invoke.Invoke();
                if (invocationResult == null || !IsTaskType(invocationResult.GetType()))
                {
                    throw new InvalidOperationException("The delegate did not return a Task."); // General awaitable support coming soon.
                }
                var awaitAdapter = AwaitAdapter.FromAwaitable(invocationResult);

                if (!awaitAdapter.IsCompleted)
                {
                    var waitStrategy = MessagePumpStrategy.FromCurrentSynchronizationContext();
                    waitStrategy.WaitForCompletion(awaitAdapter);
                }
            }

            // Future: instead of Wait(), use GetAwaiter() to check awaiter.IsCompleted above
            // and use awaiter.OnCompleted/awaiter.GetResult below.
            // (Implement a ReflectionAwaitAdapter)
            try
            {
                invocationResult.GetType().GetMethod(TaskWaitMethod, new Type[0]).Invoke(invocationResult, null);
            }
            catch (TargetInvocationException e)
            {
                IList <Exception> innerExceptions = GetAllExceptions(e.InnerException);
                ExceptionHelper.Rethrow(innerExceptions[0]);
            }
            var genericArguments = invocationResult.GetType().GetGenericArguments();

            if (genericArguments.Length == 1 && genericArguments[0].Name == VoidTaskResultType)
            {
                return(null);
            }

            PropertyInfo taskResultProperty = invocationResult.GetType().GetProperty(TaskResultProperty, TaskResultPropertyBindingFlags);

            return(taskResultProperty != null?taskResultProperty.GetValue(invocationResult, null) : invocationResult);
        }
Example #7
0
            public override object GetResult()
            {
                // Normally we would call TaskAwaiter.GetResult (https://source.dot.net/#System.Private.CoreLib/src/System/Runtime/CompilerServices/TaskAwaiter.cs)
                // We will have to polyfill on top of the TPL API.
                // Compare TaskAwaiter.ValidateEnd from Microsoft.Threading.Tasks.dll in Microsoft.Bcl.Async.nupkg.

                try
                {
                    return(_task.Result);
                }
                catch (AggregateException ex) when(ex.InnerExceptions.Count == 1)  // Task.Wait wraps every exception
                {
                    ExceptionHelper.Rethrow(ex.InnerException);

                    // If this line is reached, ExceptionHelper.Rethrow is very broken.
                    throw new InvalidOperationException("ExceptionHelper.Rethrow failed to throw an exception.");
                }
            }
Example #8
0
            public override object WaitForPendingOperationsToComplete(object invocationResult)
            {
                try
                {
                    invocationResult.GetType().GetMethod(TaskWaitMethod, new Type[0]).Invoke(invocationResult, null);
                }
                catch (TargetInvocationException e)
                {
                    IList <Exception> innerExceptions = GetAllExceptions(e.InnerException);
                    ExceptionHelper.Rethrow(innerExceptions[0]);
                }
                var args = invocationResult.GetType().GetGenericArguments();

                if (args != null && args.Length == 1 && args[0].Name == VoidTaskResultType)
                {
                    return(null);
                }

                PropertyInfo taskResultProperty = invocationResult.GetType().GetProperty(TaskResultProperty, TaskResultPropertyBindingFlags);

                return(taskResultProperty != null?taskResultProperty.GetValue(invocationResult, null) : invocationResult);
            }