Throw and Throws with printStackTrace | Selenium Forum
M
Posted on 12/06/2016
public class Test {
public static void main(String[] args) {
try {
xyz();
} catch (Exception e) {
System.out.println("error 1");
e.printStackTrace();
}
}
public static void xyz() throws Exception{
throw new Exception("Some exception");
}
}

The output of this code is:
error 1
java.lang.Exception: Some exception
at Test.xyz(Test.java:19)
at Test.main(Test.java:8)
Question: How "Some Exception" is getting printed here?
Can you please explain step by step.

M
Replied on 14/06/2016

when you call xyz() inside try/catch block java gives an exception(some exception).

but the exception is caught by try catch and e.printstacktrace outputs the error and stack trace.