C#和#x2013;尝试{}catch(异常ex){}–;不要捕获任何异常

C#和#x2013;尝试{}catch(异常ex){}–;不要捕获任何异常,c#,exception,try-catch,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"); } 未被此块捕获(“捕获”被跳过): 这是非常奇怪的行为。 完整:请看一下我制作的简短演示: class Program { static void Main(string

短版本,此方法:

public override async void MethodWithException()
{
    throw new Exception("any EXCEPTION type and format will be skipped by outer try-catch block");
}
未被此块捕获(“捕获”被跳过):

这是非常奇怪的行为。 完整:请看一下我制作的简短演示:

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....
    }
}
这是来自真实项目的简化示例。如果您有任何建议,如何使捕捉块再次正常工作,请让我知道。谢谢 这是第一次,当抓块有这样一个奇怪的行为


下载:(请在不进行调试的情况下运行,以便您可以立即看到结果)

感谢所有人的回答,特别是@MickyD与文章的链接

回答:避免异步无效

如果任何人都有相同的问题,修复了代码,所有更改都带有注释:

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
    }
}
类程序
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“1.程序启动”);//+++是,在控制台中
RealClassFromAbstract realClassFromAbstractObject=新RealClassFromAbstract();
Task.Factory.StartNew(async()=>//更改1/5:async lambda
{
尝试
{
//更改2/5:等待
等待realClassFromAbstractObject.MethodWithException();
Console.WriteLine(“控制台中没有任何内容,这是正确的”);/——控制台中没有
}
捕获(异常)
{
Console.WriteLine(“2.Nice,exception!”+exception);//+++是的,在控制台中!
}
}).配置等待(错误);
控制台中的Console.WriteLine(“3.程序结束”);//+++是
Console.ReadKey();
}
}
抽象类抽象类
{
//更改3/5:返回的类型为任务
公共抽象任务方法WithException();
}
类RealClassFromAbstract:AbstractClass
{
//更改4/5:根据Absact类返回的类型为Task
公共重写异步任务方法WithException()
{
抛出新异常(“此异常将被外部try-catch块捕获”);
//除此之外,请等待。。。。
等待任务。延迟(3);
//更改5/5:等待或:
return;//或.NET>=4.6中的“Task.CompletedTask”,如果没有等待或.NET second中的Task.FromResult()与BackgroundWorker->一起工作,则在该直线程之后,仅在->–Task之后,但在async/await之前。
在最后阶段,开发过程中出现了错误

最有趣的是,所有的东西都能正常工作至少两年,没有出现任何问题。就在上周,我在测试时遇到了奇怪的应用程序级异常。
非常感谢您提供的所有答案!

我的灵力告诉我,在调用
realClassFromAbstractObject.MethodWithException()
@RadosławCybulski之前,您需要将
wait
放在调用之前。这是我的第一个猜测,但不幸的是,这没有帮助,错误列表中有两个错误:“Acess”操作符只能在异步lambda表达式中使用。考虑用“AsiNC”修饰符标记此lambda表达式。“和”不能等待“空”。请从返回的任务中使类异步或使用<代码>。结果< /代码>。不能<代码> catch <代码>异步空隙在这里被充分记录。我引用“不欢迎。+ 1。
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
    }
}