user3165438
user3165438

Reputation: 2661

Thrown exception details

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?

Upvotes: 0

Views: 73

Answers (1)

Daniel Kelley
Daniel Kelley

Reputation: 7747

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.

Upvotes: 3

Related Questions