C# – try {} catch(Exception ex){} – 不捕获任何exception

简短版本,此方法:

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块有这么奇怪的行为时。

下载: 控制台应用程序演示项目 – https://www.dropbox.com/s/x8ta7dndbijxbvq/ConsoleAppExceptionTryCatchProblem.zip?dl=1 (请在没有调试的情况下运行,以便您可以立即看到结果)

感谢大家的答案,特别是@MickyD与文章的良好联系

答: 避免Async Void , 解释 – 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 } } 

在真正的项目中,代码的那一部分真的很老了。 第一个版本是同步的 - >第二个与BackgroundWorker一起工作 - >在直接线程之后,只在 - > - 任务之后,但在异步/等待之前。 在最后阶段,在开发过程中出现了错误。

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