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?
exc.Message
is null. This is a plain NulLReferenceException. The OP called the constructor that only accepts a parameter name – Smitty