2

Is it somehow possible to see when a NullReference exception is caught which field was null?

I know you can read the stack trace line number, but at that line there could be multiple fields that can cause the NullReference exception.

I'm afraid this isn't possible, but if so, why is this technically not possible?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kapé
  • 4,411
  • 3
  • 37
  • 54
  • 1
    Eric Lippert answered this categorically on [this question](http://stackoverflow.com/questions/8407789/can-visual-studio-tell-me-which-reference-threw-a-nullreferenceexception) – anaximander Apr 05 '14 at 15:23

3 Answers3

0

Yes, it's possible. You could scope the variables outside of the try-catch and then add them to the exception. But you should check your nullable variables for null before you even use them. Preferably always if you're not 100% sure that it cannot be null.

This code crashes at Console.WriteLine();, but now I know exactly which parameter is causing it:

int? a = (int?)null;
int? b = (int?)null;

try
{
    Console.WriteLine(a.Value + b.Value);
}
catch (Exception ex)
{
    ex.Data.Add("a", a);
    ex.Data.Add("b", a);

    throw;
}
Trafz
  • 636
  • 3
  • 13
  • Didn't know about the ex.Data.Add(), that could be useful. But in your example it requires a manual action (you need to read every possible variable) and these variables must be outside the try-catch. Also this won't work with third party code. – Kapé Apr 05 '14 at 15:29
  • The "manual action" you're talking about isn't a bad thing. Especially if you're working with 3rd-party code. I would strongly suggest that you always validate your input if you didn't generate it yourself. Always check if everything is as you expect it to be using checks and exceptions. So if you checked the input at the beginning of the method, then you know that the variables are 100% safe to have outside the try-catch scope. This way, your try-catch doesn't have a lot of code, which in turn makes it a lot easier to debug. The exceptions will then also contain the entire failed scenario. – Trafz Apr 05 '14 at 15:48
  • I'm a bit confused by the `Also this won't work with third party code.`. Because you could easily instantiate your 3rd-party classes that you're going to work with, and just set them to null before going into the try-catch. But you're right that you cannot put this code into 3rd-party libraries. You can only hope that they develop their try-catches this way too. Then their exception data would have a lot of information in the .Data, which you could read and save to your own .Data. – Trafz Apr 05 '14 at 15:53
0

You are after Code Contracts from System.Diagnostics.Contracts ns. It is present from .NET FW 4.0 I think. It evaluate pre and post conditions in your code avoiding such headache of dark NullReferenceException . You bassically do this:

Contract.Requires( x != null );

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • What about third party code? This still means you have to modify your code. But interesting for testing though. – Kapé Apr 16 '14 at 21:19
0

OK, I didn't see those questions unfortunately:

Can Visual Studio tell me which reference threw a NullReferenceException? (thanks @anaximander)

Detecting what the target object is when NullReferenceException is thrown (thanks @Rohit Vats)

These answers explain it quite well.

Community
  • 1
  • 1
Kapé
  • 4,411
  • 3
  • 37
  • 54
  • Things have moved a little today, take a look at System.Diagnostics.Contracts namespace – E-Bat Apr 05 '14 at 15:57