10

You are apparently able to rethrow an Exception without discarding the stack trace in .NET.

However it doesn't appear to be working.

Basic usage I'm following is thus:

    [WebMethod]
    public void ExceptionTest()
    {
        try
        {
            throw new Exception("An Error Happened");
        }
        catch (Exception ex)
        {
            evlWebServiceLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
            throw;
        }
    }

Problem is, the line number in the exception in the line of the throw; line, not the original throw new line.

I've tested it in a simple exe project and without the logging to the windows log line. It doesn't make any difference, the stack trace always has the wrong line number in it, making it less than useful.

Why is it doing this? How do I do it correctly?

6
  • That does not lose the stack trace. What makes you think that it does? Are you not logging or displaying ex.ToString()? Feb 6, 2014 at 10:23
  • Is the line number in the ´CallStack` of the ´InnerException´ correct?
    – Jens H
    Feb 6, 2014 at 10:24
  • Are you sure you are using throw and not throw ex? Also see stackoverflow.com/q/730250/11683 for inspiration.
    – GSerg
    Feb 6, 2014 at 10:25
  • 10
    apparently its a limitation - if a method has more than one throw you get the last line number - stackoverflow.com/questions/2493779/…
    – NDJ
    Feb 6, 2014 at 10:26
  • 2
    Sorry, I muddled up "losing stack trace" with "wrong line number". This question was indeed answered here stackoverflow.com/questions/2493779/…
    – RoboJ1M
    Feb 6, 2014 at 11:13

2 Answers 2

6

You do not lose original exception if you place it in an inner exception.

[WebMethod]
public void ExceptionTest()
{
    try
    {
        throw new Exception("An Error Happened");
    }
    catch (Exception ex)
    {
        evlWebServiceLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
        throw new Exception("Your message", ex);
    }
}
1
  • 3
    But you loose original exception type (though in your example both original and rethrown exceptions are of Exception type). ExceptionDispatchInfo.Capture(ex).Throw() throws exception of original exception type.
    – wziska
    Oct 23, 2015 at 11:27
6

I've used the following for years. Don't know if there a less "dodgy" way to achieve it in more up to date .Net frameworks though:

public void PreserveStackTrace(Exception ex)
{
    MethodInfo preserve = ex.GetType().GetMethod("InternalPreserveStackTrace",
                                                 BindingFlags.Instance | BindingFlags.NonPublic);
    preserve.Invoke(ex,null);
}

To use this:

[WebMethod]
public void ExceptionTest()
{
    try
    {
        throw new Exception("An Error Happened");
    }
    catch (Exception ex)
    {
        evlWebServiceLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
        PreserveStackTrace(ex);
        throw ex;
    }
}

Update: based on @dcastro's comment, I'd fancy an extension method in 4.5 (in < 4.5 it could still be an extension wrapping the method above):

public static void ReThrow(this Exception ex)
{
    var exInfo = ExceptionDispatchInfo.Capture(ex); 
    exInfo.Throw();
}

So you'd just have:

    catch (Exception ex)
    {
        evlWebServiceLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
        ex.ReThrow();
    }
3
  • 5
    Nowadays you can use var exInfo = ExceptionDispatchInfo.Capture(ex); exInfo.Throw();
    – dcastro
    Feb 6, 2014 at 10:32
  • @dcastro: Nice - I see that's appeared in .Net 4.5 Feb 6, 2014 at 10:35
  • 1
    ReThrow() is a great idea, but unlike "throw e;" it now gives an error saying "not all code paths return a value" - any idea how to fix properly?
    – mcmillab
    Dec 17, 2019 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.