Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print full stack trace in exception?

For example, in one place...

//---------------a try {     // some network call } catch(WebException we) {     throw new MyCustomException("some message ....", we); } 

...and in another place...

//--------------b try {     // invoke code above } catch(MyCustomException we) {     Debug.Writeline(we.stacktrace);   // <---------------- } 

The stacktrace I print, it only start from a to b, it doesnt include the inner stacktrace from the WebException.

How can I print all the stacktrace???

like image 294
jojo Avatar asked Nov 24 '10 23:11

jojo


People also ask

How do I get full stack trace of exception?

By default, the stack trace is captured immediately before an exception object is thrown. Use StackTrace to get stack trace information when no exception is being thrown.

How do I print a stack trace error?

The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.

How do I print exception stack trace in log file?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.


1 Answers

I usually use the .ToString() method on exceptions to present the full exception information (including the inner stack trace) in text:

catch (MyCustomException ex) {     Debug.WriteLine(ex.ToString()); } 

Sample output:

ConsoleApplication1.MyCustomException: some message .... ---> System.Exception: Oh noes!    at ConsoleApplication1.SomeObject.OtherMethod() in C:\ConsoleApplication1\SomeObject.cs:line 24    at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 14    --- End of inner exception stack trace ---    at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 18    at ConsoleApplication1.Program.DoSomething() in C:\ConsoleApplication1\Program.cs:line 23    at ConsoleApplication1.Program.Main(String[] args) in C:\ConsoleApplication1\Program.cs:line 13 
like image 146
Justin Avatar answered Sep 26 '22 02:09

Justin