What is a stack trace in java, how to debug exceptions in application


In this Exception tutorial we will learn what is a stack trace in java, how to debug exceptions in application i.e find out method which generated/throwed the exception, how to access stack trace information in java, understand Stack trace which consists of ‘Caused by’ i.e the concept of exception chaining.



Contents of page >
1) What is a stack trace in exceptions in java?
2) How to access stack trace information in java?
3) Understand Stack trace which consists of ‘Caused by’ (Exception chaining)>


1) What is a stack trace in exceptions in java?
A stack trace is the list of the methods from which the method is called which throws some Exception in java.

Example/ program where some method throws Exception and stack trace in formed >
package stacktracePkg;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class StackTraceExample {
   public static void main(String[] args) {
          new StackTraceExample().method1();
   }
   void method1() {
          method2();
   }
  
   void method2() {
          method3();
   }
  
   void method3() {
          throw new NullPointerException();
   }
}


Output of above stack trace program/example in java >
Exception in thread "main" java.lang.NullPointerException
   at stacktracePkg.StackTraceExample.method3(StackTraceExample.java:18)
   at stacktracePkg.StackTraceExample.method2(StackTraceExample.java:14)
   at stacktracePkg.StackTraceExample.method1(StackTraceExample.java:10)
   at stacktracePkg.StackTraceExample.main(StackTraceExample.java:6)



Now, let’s understand the above stack trace i.e find out method which throwed the exception >

Exception in thread "main" java.lang.NullPointerException
This line indicates NullPointerException was thrown in program.


Now, topmost ‘at’ tells the method which generated/throwed the exception.

at stacktracePkg.StackTraceExample.method3(StackTraceExample.java:18)
method3() throwed exception.


at stacktracePkg.StackTraceExample.method2(StackTraceExample.java:14)
method3() was called from method2()


at stacktracePkg.StackTraceExample.method1(StackTraceExample.java:10)
method2() was called from method1()


at stacktracePkg.StackTraceExample.main(StackTraceExample.java:6)
method1() was called from main method.




2) How to access stack trace information in java?
Now, we will learn to access stack trace information.

Example/ program to access stack trace information in java>
package stacktracePkg;
public class ExceptionChainingExample {
   public static void main(String[] args) {
          new ExceptionChainingExample().method1();
   }
   void method1() {
          method2();
   }
  
   void method2() {
          method3();
   }
  
   void method3() {
          try{
                 throw new NullPointerException();
          }catch (Exception cause) {
          StackTraceElement elements[] = cause.getStackTrace();
          for (int i = 0, n = elements.length; i < n; i++) {      
              System.err.println("fileName ="+elements[i].getFileName()
                         + ", method name = " + elements[i].getMethodName() + "()"
                  + ", line no =" + elements[i].getLineNumber() );
          }
          }
   }
}

Output of above stack trace program/example in java >
fileName =ExceptionChainingExample.java, method name = method3(), line no =19
fileName =ExceptionChainingExample.java, method name = method2(), line no =14
fileName =ExceptionChainingExample.java, method name = method1(), line no =10
fileName =ExceptionChainingExample.java, method name = main(), line no =6

So we can see that stack trace information has been printed in user defined format in java application.


When to use stack trace information in java?
When we want to stack trace to be printed user defined format in java.


3) Understand Stack trace which consists of ‘Caused by’ (Exception chaining)>

Stack trace may look like this -
Exception in thread "main" java.lang.RuntimeException: RuntimeException occured
   at exceptionChainingExamplePkg.ExceptionChainingExample.method3(ExceptionChainingExample.java:21)
   at exceptionChainingExamplePkg.ExceptionChainingExample.method2(ExceptionChainingExample.java:14)
   at exceptionChainingExamplePkg.ExceptionChainingExample.method1(ExceptionChainingExample.java:10)
   at exceptionChainingExamplePkg.ExceptionChainingExample.main(ExceptionChainingExample.java:6)
Caused by: java.lang.NullPointerException
   at exceptionChainingExamplePkg.ExceptionChainingExample.method3(ExceptionChainingExample.java:19)
   ... 3 more


Example/ program which generated above stack trace (the stack trace which consists of ‘Caused by’ ) >
package exceptionChainingExamplePkg;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionChainingExample {
   public static void main(String[] args) {
          new ExceptionChainingExample().method1();
   }
   void method1() {
          method2();
   }
  
   void method2() {
          method3();
   }
  
   void method3() {
          try{
                 throw new NullPointerException();
          }catch(RuntimeException e){
                 throw new RuntimeException("RuntimeException occured", e);
          }
   }
}


First I will like to tell you about Exception chaining in java. When Exception is thrown we catch it and throws some other Exception than the concept is called Exception chaining in java.

Let’s understand the above example/program in java -
When NullPointerException is thrown we catch it and a new RuntimeException exception is created with the original cause(i.e. NullPointerException ) attached and the chain of exceptions is thrown up to the next higher level exception handler in java.


Java docs says -
‘An application often responds to an exception by throwing another exception. In effect, the first exception causes the second exception. It can be very helpful to know when one exception causes another. Chained Exceptions help the programmer do this.’


Now, let’s understand the stack trace of above exception program which consists of ‘Caused by’ >
RuntimeException was thrown in program.
Now, topmost ‘at’ tells the method which generated/throwed the exception.
method3() throwed exception.
method3() was called from method2()
method2() was called from method1()
method1() was called from main method.

Below mentioned is Caused by: which tells you the actual cause of Exception. So the root cause was NullPointerException.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking on facebook, following on google+ or Twitter.

Must read for you :

No comments:

Post a Comment