2

I have C# code that's giving an exception. I have the code within a try - catch and I can see the exception but I am having a problem viewing it in VS2012. Is there a way I can send a stack trace to the console so I can look without having to right click on properties of the exception?

Even better does anyone have any code they use to log out more detailed information to the console?

2 Answers 2

3

ToString is overridden in the Exception classes and will return the stack trace.

catch(Exception ex)
{
  Console.WriteLine(ex.ToString());
}
Sign up to request clarification or add additional context in comments.

1 Comment

You don't even have to call ToString explicitly: Console.WriteLine(ex);
3

From any piece of code you can access the stack by creating a stackframe object. Lots of information in there and you don't need an exception to access it.

EG.

var callingMethod = new StackFrame(1).GetMethod().Name;

returns the calling method name.

If you have an exception as Joe Daley suggested use

Console.WriteLine(ex); to print it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.