tags:

views:

53

answers:

3

There is my customize exception class


    public class ParseFailedException : Exception
    {
            public string FailedFileName { get; set; }

            public int? LineNo { get; set; }
            public int? ColumnNo { get; set; }
    }

Is it good make the property as set, or should they be passed through the constructor method?

+10  A: 

Just pass the properties through the constructor, and set the 'set' to 'private' for the properties. It should not be possible to change the properties after throwing the exception to ensure accurate and correct information in the object.

Jan Jongboom
but there are more fields in the exception, the parameters in constructor method will be too long, how to deal with this situation?
Ji Yalin
Still better to pass in values are arguments. If too many parameters then I suggest using an "args" object. You set all the properties in the "args" object then pass that object to the contructor. This works great because if you add/remove/change the arguments of the exception, you only have to change the "args" object.
AMissico
A: 

It's easier to set by constructor (then You throw Your exception in one line), but reading should be available using properties.

Rafal Ziolkowski
+2  A: 

Exceptions are immutable objects and populated by the exception source so there should not be a public setter for your properties.

Beatles1692