Maro Maro - 23 days ago 4x
C# Question

Try Catch in object initialization

I'm reading CSV file which contain date filed
how can i ignore exception when trying to convert to DateTime?

var csvlines = File.ReadAllLines(FilePath);
// skip the first line!
var csvLinesData = csvlines.Skip(1).Select(l => l.Split(Consts.Delimiter));

return csvLinesData.Select(data => new ReleaseNote
Version = data[0],
ReleaseDate = Convert.ToDateTime(data[1]) // Possible exception


return csvLinesData.Select(data => {
   DateTime d;
   return new ReleaseNote
     Version = data[0],
     ReleaseDate = DateTime.TryParse(data[1], out d) ? d : DateTime.MinValue

You might also put null instead of DateTime.MinValue if your ReleaseDate property is nullable.