user2896152
user2896152

Reputation: 816

Get a code line in c# when an exception occurs

I know that I ask too much, but I would like to know if it's possible to detect the code that triggers an exception, not only the Line and Column Numbers but even the source code string.

For example the result of a such operation could be:

--- Exception IndexOutOfBoundsException occurs at 'myArray[4]=tempValue;'

Upvotes: 1

Views: 329

Answers (1)

Jodrell
Jodrell

Reputation: 35696

Yes, compile your code is Debug configuration.

When an exception occurs, examine the StackTrace property.

You can write an exception handler that logs this information or examine the property in your debugger.

The inherited Exception.ToString() implementation will include any stack trace information that is available.

Upvotes: 2

Related Questions