简体   繁体   English

访问异常时,过滤后的异常处理程序中的代码将引发NullReferenceException

[英]Code in filtered exception handler throws NullReferenceException when accessing exception

When I compile a UWP app with the .NET Native compiler and turn on code optimizations (essentially release mode), then I get a NullReferenceException when I try to access the actual exception in the catch block. 当我使用.NET Native编译器编译UWP应用并打开代码优化(本质上是发布模式)时,当我尝试访问catch块中的实际异常时,会收到NullReferenceException

Code Sample: 代码示例:

try
{
    throw new ArgumentNullException("Param");
}
catch (ArgumentNullException ex) when (ex.ParamName == "Param")
{
    ErrorBlock.Text = ex.ParamName; // ErrorBlock is a TextBlock in the xaml
}
catch (Exception)
{
}

It goes into the correct catch block, and throws a NullReferenceException when I access ex . 它进入正确的catch块,并在我访问ex时引发NullReferenceException This only fails if both .Net Native and code optimizations are on. 只有同时启用.Net本机优化和代码优化,此操作才会失败。

What causes this issue? 是什么原因导致此问题?

I am not exactly sure why it is going wrong (have been debugging for quite some time now), but the lack of await made me curious. 我不确定为什么会出错(现在已经调试了很长时间),但是缺少await使我感到好奇。

If you do await the ShowAsync method the code runs without a problem (obviously you need to make the method async if you didn't do that yet): 如果您确实等待ShowAsync方法,则代码可以正常运行(显然,如果您尚未这样做,则需要使该方法async ):

await new MessageDialog("Argument null exception: " + argEx.Message).ShowAsync();

While the code block without the await failed. 而没有await代码块失败。 Not sure if this is a bug or something you should have fixed... 不知道这是错误还是应该修复的问题...

I work on the .NET Native runtime and compiler team. 我在.NET Native运行时和编译器团队中工作。

This is a bug inside of our compiler. 这是我们编译器内部的错误。 You can think of each exception handling region (try, catch, finally, when) as a small function or "funclet". 您可以将每个异常处理区域(尝试,捕获,最终,何时)视为一个小函数或“ funclet”。 We lose track of the exception object when setting up the stack for the "when" (aka filter block). 当为“ when”(又称过滤器块)设置堆栈时,我们将无法跟踪异常对象。 This bug is corrected in Windows Tools 1.3 which, given no major setbacks, should be shipping in another week or two. Windows工具1.3中已纠正了该错误,在没有重大挫折的情况下,该工具应在另外一两周内发货。 It'll show up as an update for folks that have installed VS 2015 Update 2. 对于已安装VS 2015 Update 2的人员,它将显示为更新。

Let me know if you have any other questions. 如果您还有其他问题,请告诉我。

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

相关问题 安装程序有回调时 Moq 抛出 NullReferenceException 异常 - Moq throws NullReferenceException exception when setup has a callback 我的c#代码在访问sql server时抛出异常 - my c# code throws an exception on accessing sql server 访问注册表项会引发异常吗? - accessing a registry key throws an exception? 异常处理程序代码段 - Exception Handler Code Snippet 访问局部变量时,匿名方法引发运行时异常 - Anonymous Method throws runtime exception when accessing local variable Windows 窗体崩溃:在全局异常处理程序中捕获到意外的 NullReferenceException - Windows Forms Crash : Unexpected NullReferenceException caught in Global Exception Handler 处理异常时,LoginView标签上的NullReferenceException - NullReferenceException on a LoginView Label when exception is handled 任务中的F#本机调用抛出异常,绕过异常处理程序 - F# native call in Task throws exception, bypasses exception handler HttpWebRequest在代码中引发异常,但浏览器未引发 - HttpWebRequest throws exception in code but not browser 使用 ManagedIdentity 访问 AzureKeyVaults 会引发零星异常 - Accessing AzureKeyVaults using ManagedIdentity throws sporadic exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM