C# - try {} catch(Exception ex){} - 不捕获任何异常

时间:2017-07-16 17:00:30

标签: c# exception try-catch

简短版本,此方法:

public override async void MethodWithException()
{
    throw new Exception("any EXCEPTION type and format will be skipped by outer try-catch block");
}

未被此块捕获(跳过“catch”):

try
{
    realClassFromAbstractObject.MethodWithException();

    Console.WriteLine("Output in the console – NOT POSSIBLE but true!");

}
catch (Exception exception)
{

     //Nothing caught!
     Console.WriteLine("2. Nothing in console, skipped exception! " + exception); //--- Notihng in the output

}

这是一种超级奇怪的行为。

完整:请看一下我做的简短演示:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("1. Program starts"); //+++ Yes, in the console
        RealClassFromAbstract realClassFromAbstractObject = new RealClassFromAbstract();


        Task.Factory.StartNew(() =>
        {
            try
            {


                //Next method should to throw an exception! But nothing!
                realClassFromAbstractObject.MethodWithException();



                Console.WriteLine("In the console too – NOT POSSIBLE but true!"); //+++ Yes, in the console

            }
            catch (Exception exception)
            {

                //Nothing caught!
                Console.WriteLine("2. Nothing in console, skipped exception! " + exception); //--- Notihng in the output

            }
        }).ConfigureAwait(false);



        Console.WriteLine("3. Program ends"); //+++ Yes, in the console
        Console.ReadKey();
    }
}

abstract class AbstractClass
{
    public abstract void MethodWithException();
}

class RealClassFromAbstract : AbstractClass
{
    public override async void MethodWithException()
    {
        throw new Exception("any EXCEPTION type and format will be skipped by outer try-catch block");
        throw new ArgumentException();
        throw new DivideByZeroException();


        //Anythig else, await....
    }
}

这是真实项目的简化示例。如果你有任何建议如何让catch挡板重新工作,像往常一样,请告诉我。谢谢! 这是第一次,当catch块有这么奇怪的行为时。

下载:console application demo project – https://www.dropbox.com/s/x8ta7dndbijxbvq/ConsoleAppExceptionTryCatchProblem.zip?dl=1(请在没有调试的情况下运行,以便您可以立即看到结果)

1 个答案:

答案 0 :(得分:1)

感谢大家的回答,特别是@MickyD与文章的重要链接

答案:避免异步无效explanations - https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

如果有人会遇到同样的问题,修改了代码,所有更改都带有注释:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("1. Program starts"); //+++ Yes, in the console
        RealClassFromAbstract realClassFromAbstractObject = new RealClassFromAbstract();


        Task.Factory.StartNew(async () =>//CHANGE 1/5: async lambda
        {
            try
            {
                //CHANGE 2/5: await
                await realClassFromAbstractObject.MethodWithException();


                Console.WriteLine("Nothing in the console, that's correct"); //--- Notihng in the console

            }
            catch (Exception exception)
            {
                Console.WriteLine("2. Nice, exception! " + exception); //+++ Yes, in the console!

            }
        }).ConfigureAwait(false);



        Console.WriteLine("3. Program ends"); //+++ Yes, in the console
        Console.ReadKey();
    }
}

abstract class AbstractClass
{
    //CHANGE 3/5: returned type is Task
    public abstract Task MethodWithException();
}

class RealClassFromAbstract : AbstractClass
{
    //CHANGE 4/5: returned type is Task according to the abstact class
    public override async Task MethodWithException()
    {
        throw new Exception("This exception would be caught by outer try-catch block");


        //Anythig else, await....
        await Task.Delay(3);


        //CHANGE 5/5: await or:
        return;//or "Task.CompletedTask" in .NET >=4.6 if no awaits or Task.FromResult() in .NET <4.6
    }
}

在实际项目中,代码的那一部分真的很旧。第一个版本是同步的 - &gt;第二个与BackgroundWorker一起工作 - &gt;在那个直线程之后并且仅在 - &gt; - 任务之后,但在异步/等待之前。 在最后阶段,在开发过程中出现了错误。

最有意思的是,一切都工作了至少两年没有问题。我在上周测试时只得到了奇怪的应用程序级异常。 非常感谢您的所有答案!