Question

try
{
     list = from XElement e in d.Descendants(wix + "File")
            where e.Attribute("Name").Value.Contains(temp.Name) &&
            e.Parent.Parent.Attribute("Name").Value.Contains(temp.Directory.Name)
            select e;
}
catch (NullReferenceException e)
{
     MessageBox.Show(e.Message);
}
catch (Exception e)
{
     MessageBox.Show(e.Message);
}

now my question is why does this code produce a run time error saying I have a NullReferenceException unhandled. If you need more information about the program let me know.

EDIT: The debugger points to the "where" part of the linq statement. When I run this program direct from the exe file I still get the exception.

Was it helpful?

Solution

EDIT: Okay, I think I know the problem... it's due to deferred query execution.

If you've just got the query construction in the try/catch block, that's not going to catch exceptions which occur while the query is being executed.

Look at the stack trace you've got, and you'll find that there'll be a stack frame where you're executing the query - it's just the auto-generated lambda expression which comes from this bit of code, and it's not running within the scope of the try/catch block.

ORIGINAL ANSWER:

Are you just seeing the exception in the debugger? If so, go into the debugger exception dialog and change the preferences for the point at which exceptions cause the debugger to "break". The catch block should be handling the NullReferenceException normally. (Admittedly I don't think you should really be catching NullReferenceException in the first place, and catching all exceptions like that is generally a bad idea too, other than at the top of the stack - but that's a different matter.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top