What is the e.printStackTrace () method in Java?
Well, the purpose of using this method is e.printStackTrace(); is to see what exactly is wrong.
For example, we want to handle an exception. Let's look at the following example.
public class Main{ public static void main(String[] args) { int a = 12; int b = 2; try { int result = a / (b - 2); System.out.println(result); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } } }
I used the e.printStackTrace(); method e.printStackTrace(); to show exactly what is wrong.
At the output, we can see the following result.
Error: / by zero java.lang.ArithmeticException: / by zero at Main.main(Main.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Sageer Shahzad Feb 26 '17 at 0:16 2017-02-26 00:16
source share