Can I customize the properties of my custom exception?

There is my class exception class


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

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

      

Is it okay to use the property as given or should it be passed through a constructor method?

+2


source to share


3 answers


Just pass properties through the constructor and set 'set' to 'private' for properties. It is not possible to change properties after throwing an exception to provide accurate and correct information in the object.



+10


source


Exceptions are immutable objects and are populated with an exception source, so there should be no public customizer for your properties.



+2


source


It is easier to set a constructor (then you throw your exception on one line), but reading should be available using properties.

0


source







All Articles