简体   繁体   English

为什么我的自定义异常被 AggregatException 包裹。 这取决于什么?

[英]Why is my custom exception wrapped by an AggregatException. What does this depend on?

I have the following custom exception :我有以下自定义异常:

 public class MyCustomException : Exception
    {
        public MyCustomException(string message) : base(message)
        { }

    }

I throw it at this point:我在这一点上抛出它:

 private async Task<string> AquireToken()
        {
            string url = GetUrl("authentication/connect/token");
           
//...
 private static string GetUrl(string relativeUrl)
        {
            var baseUrl = Environment.GetEnvironmentVariable("BASE_URL");
            if (string.IsNullOrEmpty(baseUrl))
            {
                throw new MyCustomException("Address is not set in Enviroment Variable (BASE_URL)");
            }
            var fullUrl = $"{baseUrl.Trim('/')}/{relativeUrl}";
            return fullUrl;
        }

But when testing, I find that it is wrapped by an AggregateException, the test fails:但是测试的时候发现被一个AggregateException包裹了,测试失败:

 MyCustomException exception = await Assert.ThrowsAsync<MyCustomException>(async () =>
            {
                Environment.SetEnvironmentVariable("BASE_URL", null);
                await serviceUnderTest.SampleMethod(input);
            });
Assert.Throws() Failure
Expected: typeof(SAMPLECOMPANY.SAMPLEPROJECT.SampleMicroservice.WebApi.Service.Exceptions.MyCustomException)
Actual:   typeof(System.AggregateException): One or more errors occurred. (Address is not set in Enviroment Variable (BASE_URL))
---- System.AggregateException : One or more errors occurred. (Address is not set in Enviroment Variable (BASE_URL))
-------- SAMPLECOMPANY.SAMPLEPROJECT.SampleMicroservice.WebApi.Service.Exceptions.MyCustomException : Address is not set in Enviroment Variable (BASE_URL)

At other places in the same class I throw it too (for example in the method SampleMethod() that calls AquireToken() and, there I only get the custom exception.在同一个类的其他地方,我也抛出了它(例如,在调用AquireToken()的方法SampleMethod()中,我只得到自定义异常。

I am confused, because in other projects I do supposedly analogous and there the exceptions are not wrapped....我很困惑,因为在其他项目中我做了类似的事情,并且没有包装异常......

What does it depend on, if the exception of AggregateException is wrapped or not, how can I avoid it?它取决于什么,如果包裹了 AggregateException 的异常,我怎样才能避免它?

You are starting a task here by using await/aync and you are not handling exception.您在此处使用 await/aync 开始一项任务,并且您没有处理异常。 That is why you are getting aggregate exception.这就是为什么您会收到聚合异常的原因。 From MS docs来自 MS 文档

https://docs.microsoft.com/en-us/dotnet/api/system.aggregateexception.handle?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.aggregateexception.handle?view=netframework-4.7.2

 MyCustomException exception = await Assert.ThrowsAsync<MyCustomException>(async () =>
        {
            Environment.SetEnvironmentVariable("BASE_URL", null);
            await serviceUnderTest.SampleMethod(input);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM