-1

I have caught an exception and after catching it I have to append the method name so that I should know which method the error came from, and then throw it to another function and save it in database.

try
{
}
catch (Exception ex)
{
    string strError = ex.Message.ToString() + "methodname:getNoOfRecordsForBatchProcess";
    throw strError.ToString();
}  

but it gives me error that you can't use string variable to throw exception.the throw exception only use with system exception. is there any way to handle this error.

5 Answers5

5

The method name is visible in Exception.StackTrace property too.

By the way you may rely on some other way to recover its name using StackFrame Class, like for example:

        private static string GetCallingMethodName()
        {
            const int iCallDeepness = 2; //DEEPNESS VALUE, MAY CHANGE IT BASED ON YOUR NEEDS
            System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(false);
            System.Diagnostics.StackFrame sframe = stack.GetFrame(iCallDeepness);
            return sframe.GetMethod().Name;
        }
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I think creating StackTrace is bit expense. But here it is not an issue, cuz already an exception happened. – Murali Murugesan Feb 19 '13 at 10:41
  • In the below referred response there's the TargetSite Exception property, but the only 100% accurate way to go is using the StackFrame, so +1 – Ricardo Rodrigues Feb 19 '13 at 10:45
  • This code is DEEPNESS dependent (as I mentioned in answer), so you just call it, in catch block, but it's better to have it in some Log API, so you always sure on what deepness it will be called. – Tigran Feb 19 '13 at 10:45
1

An answer to your question:

throw new Exception(strError);

(However, as others have said, this might not be the best way to handle this.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
0

Possible duplicate of How to get the name of the method that caused the exception.

catch (Exception ex)
{
   MethodBase site = ex.TargetSite;
   string methodName = site == null ? null : site.Name;
   ...           
}
Community
  • 1
  • 1
Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
0

When catching and rethrowing an exception it's 'recommended' to create a new Exception, with a meaningful error message, and pass the original exception in as the inner exception.

For example:

catch(Exception ex)
{
  const string message = "Some error message.";
  throw new MeaningfulException(message, ex);
}
robert.oh.
  • 644
  • 2
  • 5
  • 13
0

I would also go for the StackFrame. I'm posting an extension to @Tigran's answer (because you asked for a bit more clarified usage inside the try{...}catch{...} block), so if this is helping you to understand the usage, please accept his answer, not mine:

try
{
    int a = 0;
    var r = 1 / a;
}
catch (Exception ex)
{
    throw new Exception(
        String.Format("{0} Method name: {1}",
            ex.Message,
            GetCallingMethodName()),
        ex);
}

The GetCallingMethodName:

private static string GetCallingMethodName()
{
    const int iCallDeepness = 1; //DEEPNESS VALUE, MAY CHANGE IT BASED ON YOUR NEEDS
    System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(false);
    System.Diagnostics.StackFrame sframe = stack.GetFrame(iCallDeepness);
    return sframe.GetMethod().Name;
}

P.S. @Tigran, I will remove this answer if you consider that it's not needed.

Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78