Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 715
FUR10N Avatar asked Apr 05 '16 14:04

FUR10N


2 Answers

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...

like image 197
Patrick Hofman Avatar answered Nov 20 '22 11:11

Patrick Hofman


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.

like image 31
MattWhilden Avatar answered Nov 20 '22 10:11

MattWhilden