Cannot Identify the property which throw exception when using Object initializer in C#
Asked Answered
E

1

8

In the below two sample code I am trying to instantiate a class named Test by using C# normal method and Object initializer.

DateTime? nullDate = null; //this value will come from somewhere else
DateTime? notNullDate = DateTime.Now;
var test = new Test();
test.Date = nullDate.Value; //exception will throw here
test.Name = "String";
test.AnotherDate = notNullDate.Value;

In the above sample code, I can clearly understand which property is showing exception while debugging.

DateTime? nullDate = null; //this value will come from somewhere else
DateTime? notNullDate = DateTime.Now;
var test = new Test
{
    Date = nullDate.Value,
    Name = "String",
    AnotherDate = notNullDate.Value
};

In this above code, when I use object initializer I couldn't understand which property is thrown exception. Here I couldn't debug line by line. If I have lot of properties initialized it's very difficult to identify.

Here is my question: How can I identify which property is showing exception from exception window? Right now the inner exception is null.

enter image description here

Evanston answered 23/11, 2016 at 15:55 Comment(5)
You can't - which is why you should do null checks, etc. before calling the initializer. The exception does not give you any context about what is nullDisbelief
@DStanley I can check null reference but my question is no other way to identify the property which throw exception?Evanston
No - the exception gives you no context about what is null.Disbelief
@DStanley let's suppose these values are the parameters of service and my property is not nullable , there could be lot of chance to become null from the client side.in this case I have to look every property what is null or not.Evanston
Yes you do, unless the service architecture has validation for required parameters built-in (like web services does).Disbelief
F
0

Object initializers should be used for simple initialization. Where you have a code that throws an exception, you will run into described by you issues.

I know this is not really an answer, but you will not know which property fails. In case of nullables you could use something like this, where you specify the default value.

var test = new Test
        {
            Date = nullDate.GetValueOrDefault(new DateTime()),
            Name = "String",
            AnotherDate = notNullDate.Value
        };
Fassett answered 23/11, 2016 at 16:3 Comment(4)
My question is all about , is there any debugging techniques to identify the property? then what is the advantage of using Object Initializers, both not making any difference for me?Evanston
You can refer to this answers for benefits: #12842871Fassett
I am wondering why visual studio can't provide the property which throw exception.Evanston
No, because from a VS point of view that's one line of code. You have the same problem if you do something like prop1.prop2.prop3.ToString(). If any one of those properties is null you will get an exception with no context as to which one is null. The exception handler does not go to the trouble of examining the call stack, local variables, member names, etc. to give you more context. If you want it to, then you can request a feature on connect.microsoft.com, but null checks are the standard way to deal with that problem.Disbelief

© 2022 - 2024 — McMap. All rights reserved.