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.
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
. This only fails if both .Net Native and code optimizations are on.
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.
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):
await new MessageDialog("Argument null exception: " + argEx.Message).ShowAsync();
While the code block without the await
failed. Not sure if this is a bug or something you should have fixed...
I work on the .NET Native runtime and compiler team.
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". We lose track of the exception object when setting up the stack for the "when" (aka filter block). This bug is corrected in Windows Tools 1.3 which, given no major setbacks, should be shipping in another week or two. It'll show up as an update for folks that have installed VS 2015 Update 2.
Let me know if you have any other questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With