What is an exception stack trace?
Exception stack trace is a combination of exception name, description and call stack. The stack trace contains all method calls from the start of a thread to till the point where exception raised. The stack trace is produced automatically by the Java Virtual Machine.
A stack trace is a very helpful debugging tool, Because it doesn’t only shows what the error occurred and where the error occurred, but also shows the stack of methods that were called up to the point where exception occurred.
StackTraceExample : Here we are printing stack trace with public void printStackTrace()
method of an Exception class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.vidvaan.corejava.exception03.stackTrace; // Exception Stack trace public class ExceptionStackTraceExample { public static void main(String[] args) { try { method1(); } catch (Exception e) { // It will print exception stack trace to console e.printStackTrace(); } } static void method1() { method2(); } static void method2() { method3(); } static void method3() { method4(); } static void method4() { throw new IllegalArgumentException("some error"); } } |
Output :
java.lang.IllegalArgumentException: some error
at com.vidvaan.corejava.exception03.stackTrace.ExceptionStackTraceExample.method4(ExceptionStackTraceExample.java:28)
at com.vidvaan.corejava.exception03.stackTrace.ExceptionStackTraceExample.method3(ExceptionStackTraceExample.java:24)
at com.vidvaan.corejava.exception03.stackTrace.ExceptionStackTraceExample.method2(ExceptionStackTraceExample.java:20)
at com.vidvaan.corejava.exception03.stackTrace.ExceptionStackTraceExample.method1(ExceptionStackTraceExample.java:16)
at com.vidvaan.corejava.exception03.stackTrace.ExceptionStackTraceExample.main(ExceptionStackTraceExample.java:8)
The stack trace is encapsulated into an array of java.lang.StackTraceElement
. This array returned by Throwable.getStackTrace()
method. Each element represents a single stack frame in the call stack. StackTraceElement encapsulates following information.
1 2 3 4 |
public StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber); |
How to Convert exception stack trace into string
Some times we may required to convert the stack trace to java.lang.String format. Below is an example to convert stack trace into String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.vidvaan.corejava.exception03.stackTrace; import java.io.PrintWriter; import java.io.StringWriter; // To convert exception stack trace to java.lang.String public class StackTraceIntoStringExample { public static void main(String[] args) { try { method1(); } catch (Exception e) { StringWriter outError = new StringWriter(); e.printStackTrace(new PrintWriter(outError)); String errorString = outError.toString(); System.out.println("Stack trace in string format : " + errorString); } } static void method1() { method2(); } static void method2() { method3(); } static void method3() { method4(); } static void method4() { throw new IllegalArgumentException("some error"); } } |
How to Log exceptions in Projects ?
In Project we never ever use public void printStackTrace() to print stack trace. Because this will print the stack trace into console which is not permanent for future references. In project we always print the stack traces into log files.
The good news is that most loggers, including Log4j and Logback, will write exceptions with stack traces into log files if you call them with the right arguments.
If you use SLF4J or Log4j or Logback, Logging error code looks like as follows:
1 |
logger.error(“Something wrong happened:”, exceptionObject); |