Breaking News
Home / Java / Java Snippets / Get StackTrace as String

Get StackTrace as String

Java StackTrace

Its easy to print the stack trace using e.printStackTrace() where e is instance of an Exception/Throwable. This prints the trace to the Standard Error stream. But how about if you want to have the stacktrace stored in file or database or want to search as text.

This snippets shows how to get the stack trace as String.

 

The output is as follows:

java.lang.RuntimeException: Exception : some thing wrong here!
at com.livrona.snippets.system.PrintStackTrace.methodC(PrintStackTrace.java:75)
at com.livrona.snippets.system.PrintStackTrace.methodB(PrintStackTrace.java:71)
at com.livrona.snippets.system.PrintStackTrace.methodA(PrintStackTrace.java:63)
at com.livrona.snippets.system.PrintStackTrace.exec(PrintStackTrace.java:56)
at com.livrona.snippets.system.PrintStackTrace.main(PrintStackTrace.java:25)

[pb_builder]

Check Also

How to progamatically get the Java version?

Snippet The key here is to use the system property java.version and get minor and …

Advertisment ad adsense adlogger