0

Here I have multiple string variables assigned but I want to catch the error at which variable & its name.

try
{
    sSrNo = Convert.ToString(valueArrayCustomerDetails[row, 1]);
    sNotice_No = (string)valueArrayCustomerDetails[row, 2];
    dt_notice_date = Convert.ToDateTime(valueArrayCustomerDetails[row, 3]);
    sAgreement_No = Convert.ToString(valueArrayCustomerDetails[row, 4]);
    sBorrower = (string)valueArrayCustomerDetails[row, 5];
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " Column-"); // here i want variable name at which error occurs
    throw;
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
vinod k
  • 33
  • 3
  • Maybe look into `TryParse` methods and or wrap each assignment in a `try/catch` if you want to handle the case for each variable. – Trevor Jul 16 '21 at 13:10
  • 2
    Most obvious is to use multiple `try/catch` for each variable you are trying to parse. – Sinatr Jul 16 '21 at 13:11
  • @Sinatr Yes, I don't want use multiple 'try/catch' for each variable. Please advice. – vinod k Jul 16 '21 at 13:14
  • 2
    It's [very easy](https://dotnetfiddle.net/5UEjnI) to refactor. – Sinatr Jul 16 '21 at 13:19
  • 1
    I suggest you look at the exceptions and try to prevent them from occurring in the first place, for example using `TryParse` `as string` and similar – Charlieface Jul 16 '21 at 14:57

3 Answers3

2

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}";
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Wouldn't avoiding costly exceptions altogether be the way to go? – Uwe Keim Jul 16 '21 at 18:21
  • 2
    @UweKeim they are just costly if they happen often. I dont know anything about OP's use case or if there are exceptions at all. So i focused on the question itself – Tim Schmelter Jul 16 '21 at 18:23
1
string index="1";
try
{
    sSrNo = Convert.ToString(valueArrayCustomerDetails[row, 1]);
index="2";
sNotice_No = (string)valueArrayCustomerDetails[row, 2];
index="3";
dt_notice_date = 
Convert.ToDateTime(valueArrayCustomerDetails[row, 3]);
index="4";
sAgreement_No = 
Convert.ToString(valueArrayCustomerDetails[row, 4]);
index="5";
sBorrower = (string)valueArrayCustomerDetails[row, 5];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " 
Column-"+index); // here i want variable name at which error occurs
throw;
}
Jason N
  • 109
  • 3
0

Try using this:

It will give the line number at which the exception is thrown thus getting the variable.

catch(Exception ex)
{
var LineNumber = new StackTrace(ex, true).GetFrame(0).GetFileLineNumber();

MessageBox.Show(ex.StackTrace + "\n Error at :- Row-" + LineNumber);
}

ex.StackTrace gives detailed error message.

Vivek Kumar
  • 21
  • 1
  • 7
  • A possible [improvement](https://stackoverflow.com/q/12556767/1997232) to show *index* as OP want. – Sinatr Jul 16 '21 at 13:34