Skip to content

Instantly share code, notes, and snippets.

@mikeblakeuk
Last active November 7, 2019 15:58
Show Gist options
  • Save mikeblakeuk/c45156de2d1e362e66b3638e343b551a to your computer and use it in GitHub Desktop.
Save mikeblakeuk/c45156de2d1e362e66b3638e343b551a to your computer and use it in GitHub Desktop.
GetBaseException vs InnerException
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Machine.Specifications;
// ReSharper disable PossibleNullReferenceException when getting InnerException from AggregateException
namespace Tests
{
[Subject(typeof(AggregateException))]
public class AggregateExceptionTests
{
It should_handle_exceptions = () =>
{
var taskException = new ArithmeticException("test1");
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException));
exception.GetBaseException().Message.ShouldEqual("test1");
exception.InnerException.Message.ShouldEqual("test1");
};
It should_handle_AggregateException_exceptions = () =>
{
var taskException = new AggregateException(new ArithmeticException("test1"));
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException));
exception.GetBaseException().Message.ShouldEqual("test1");
exception.InnerException.Message.ShouldEqual("One or more errors occurred.");
};
It should_handle_AggregateException_with_AggregateException_exception = () =>
{
var taskException = new AggregateException(new AggregateException(new ArithmeticException("test1")));
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException));
exception.GetBaseException().Message.ShouldEqual("test1");
exception.InnerException.Message.ShouldEqual("One or more errors occurred.");
};
It should_handle_AggregateException_with_multi_inner_exceptions = () =>
{
var taskException = new AggregateException(new ArithmeticException("test1"), new ArithmeticException("test2"));
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException));
var multipleExceptions = exception.GetBaseException() as AggregateException;
exception.Message.ShouldEqual("One or more errors occurred.");
exception.InnerException.Message.ShouldEqual("One or more errors occurred.");
multipleExceptions.Message.ShouldEqual("One or more errors occurred.");
multipleExceptions.GetBaseException().Message.ShouldEqual("One or more errors occurred.");
multipleExceptions.InnerExceptions[0].Message.ShouldEqual("test1");
multipleExceptions.InnerExceptions[1].Message.ShouldEqual("test2");
multipleExceptions.InnerException.Message.ShouldEqual("test1");
};
It should_handle_AggregateException_with_inner_inner_exceptions = () =>
{
var taskException = new AggregateException(new ArithmeticException("test1", new ArithmeticException("test1a")));
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException));
exception.GetBaseException().Message.ShouldEqual("test1");
exception.InnerException.Message.ShouldEqual("One or more errors occurred.");
};
public static void AsyncThrow(Exception exception)
{
Throw(exception).Wait();
}
public static async Task Throw(Exception exception)
{
await Task.Run(() => { throw exception; });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment