문제

When my application fails, a catch block shows a messageBox with the exception stack trace, so that I could easily see the number of line that fails.
The try block includes the main method which invokes the others.

catch (Exception e)
{
    DetailsTextBox.Text = e.StackTrace;
}

Sometimes, a method from another class (not the main) fails.
Since I would like to show the same messageBox, from the catch block there I throw the exception to the main class:

catch (Exception ex)
{
    throw (ex);
}  

My problem:
Now the line number shown by the stack trace is the line from which I throw the exception, not the line where the application fails.

Any ideas?

도움이 되었습니까?

해결책

You should just use throw not throw (ex) as that will lose the stack trace.

However, as John Saunders pointed out you might be better of not catching the exception in the first place, although there are certain scenarios where this can be useful.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top