gudii9
asked on
chaining exceptions
Hi,
I was working on below example
http://www.avajava.com/tutorials/lessons/how-do-i-chain-exceptions.html?page=2
I got output as below
I have not clearly understood how the chaining happening here and the output
please advise
Any links resources ideas highly appreciated. Thanks in advance
I was working on below example
http://www.avajava.com/tutorials/lessons/how-do-i-chain-exceptions.html?page=2
I got output as below
***no chaining example:
java.lang.Exception: Two
at ExceptionTest2.main(ExceptionTest2.j ava:11)
***chaining example 1:
java.lang.Exception: Four
at ExceptionTest2.main(ExceptionTest2.j ava:22)
Caused by: java.lang.Exception: Three
at ExceptionTest2.main(ExceptionTest2.j ava:20)
###what was the cause:
java.lang.Exception: Three
at ExceptionTest2.main(ExceptionTest2.j ava:20)
***chaining example 2:
java.lang.Exception: java.lang.Exception: Five
at ExceptionTest2.main(ExceptionTest2.j ava:35)
Caused by: java.lang.Exception: Five
at ExceptionTest2.main(ExceptionTest2.j ava:33)
I have not clearly understood how the chaining happening here and the output
please advise
Any links resources ideas highly appreciated. Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
System.out.println("***no chaining example:");
try {
try {
throw new Exception("One");
} catch (Exception e) {
throw new Exception("Two");
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
In the code above... "One" does not print, because "One" is a new exeception that is thrown. Exeception "One" is caught by the following catch.
} catch (Exception e) {
throw new Exception("Two");
}
So now you have to ask... one does this catch do? Does it print anything about "One"... no it does not. It simply throws another new exception called "Two".
Exception "Two" is then caught and printed by the following line:
e.printStackTrace(System.o
the difference between e.printStackTrace(System.o
and e.getCause().printStackTra
the getcause only prints the cause of the exception, it doesn't print the whole "stack" of errors.
for example in chaining example 1:
e.printStackTrace(System.o
java.lang.Exception: Four
at test.ExceptionTest.main(Ex
Caused by: java.lang.Exception: Three
at test.ExceptionTest.main(Ex
e.getCause().printStackTra
java.lang.Exception: Three
at test.ExceptionTest.main(Ex
ASKER
I wonder why "One" did not printed.
also what is difference between below lines
I see both yielding same out put.
what is the purpose of e.getCause()
Please advise