Well, you have to use multiple try-catch if you want that information:
try
{
sSrNo = Convert.ToString(valueArrayCustomerDetails[row, 1]);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " 1");
throw;
}
try
{
sNotice_No = (string)valueArrayCustomerDetails[row, 2];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " 2");
throw;
}
// ...
Another option is to create a helper method like this:
public static class Utils
{
public static T TryDo<T>(Func<T> returnMethod, Action<Exception> exceptionAction = null, bool throwOnException = true, T fallbackValue = default(T))
{
try
{
return returnMethod();
}catch(Exception ex)
{
exceptionAction(ex);
if(throwOnException)
{
throw;
}
}
return fallbackValue;
}
}
Now you can use this concise and readable code:
sSrNo = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 1]), ex => MessageBox.Show(GetMessage(ex, row, 1)));
sNotice_No = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 2]), ex => MessageBox.Show(GetMessage(ex, row, 2)));
dt_notice_date = Utils.TryDo(() => Convert.ToDateTime(valueArrayCustomerDetails[row, 3]), ex => MessageBox.Show(GetMessage(ex, row, 3)));
sAgreement_No = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 4]), ex => MessageBox.Show(GetMessage(ex, row, 4)));
sBorrower = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 5]), ex => MessageBox.Show(GetMessage(ex, row, 5)));
// local method
string GetMessage(Exception ex, int row, int col)
=> $"{ex.ToString()} Error At :- Row-{row} Column-{col}";