InterviewSolution
Saved Bookmarks
| 1. |
Print stack trace information in Java |
|
Answer» A stack TRACE is a characterization of a call stack at a particular instant, with each element depicting a method call statement. The stack trace contains all the call statements from the START of a thread until the POINT of generation of exception. When the stack trace is printed, the point of generation is exception is printed first, followed by method call statements, which help us identify the root cause of failure. Here is an example of printing the stack trace in Java: PUBLIC class Example { public STATIC void main (String args[]) { int arr[] = {1,2,3,4}; int num1=10, num2=0; int ans; try { System.out.println("The output is..."); ans = num1/num2; System.out.println("The result is " +ans); } catch (ArithmeticException ex) { ex.printStackTrace(); } } }The output is as follows: $javac Example.java $java Example The output is... java.lang.ArithmeticException: / by zero at Example.main(Example.java:11) |
|