e.g I'm parsing whole Excel file with many rows, that has an column which contains Date.
I'm not sure how to handle error-handling when it comes to parsing string to DateTime
Here's an sample code which is in C#
for (int i = 1; i < sheet.RowsCount; i++)
{
// cells are strings
var cells = sheet.GetRow(i).Cells;
(...)
if (!DateTime.TryParse(cells[1], out DateTime date))
{
Console.WriteLine($"Unable to parse DateTime for row {i}");
continue; // <--- here, basically skip current on fail and go to next.
}
}
for (int i = 1; i < sheet.RowsCount; i++)
{
// cells are strings
var cells = sheet.GetRow(i).Cells;
(...)
if (!DateTime.TryParse(cells[1]), out DateTime date))
{
throw new Exception($"Problem with parsing DateTime at {i} row"); // <--- here
}
}
Is 1st approach ok to do that?
Isn't it something like "hidden behaviour"? It may be confusing that e.g file contains 300 rows, but my function returned only 270.
Should my program yell loudly when it fails (with an exception) or just perform its job "properly", with "silent" Console Logs?