Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top