Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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

***no chaining example:
java.lang.Exception: Two
      at ExceptionTest2.main(ExceptionTest2.java:11)

***chaining example 1:
java.lang.Exception: Four
      at ExceptionTest2.main(ExceptionTest2.java:22)
Caused by: java.lang.Exception: Three
      at ExceptionTest2.main(ExceptionTest2.java:20)
###what was the cause:
java.lang.Exception: Three
      at ExceptionTest2.main(ExceptionTest2.java:20)

***chaining example 2:
java.lang.Exception: java.lang.Exception: Five
      at ExceptionTest2.main(ExceptionTest2.java:35)
Caused by: java.lang.Exception: Five
      at ExceptionTest2.main(ExceptionTest2.java: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
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

That makes more sense.

I wonder why "One" did not printed.

also what is difference between below lines

     e.printStackTrace(System.out);
            
                  e.getCause().printStackTrace(System.out);

I see both yielding same out put.
what is the purpose of e.getCause()

Please advise
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);
		}

Open in new window

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");
			}

Open in new window


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.out);

the difference between   e.printStackTrace(System.out);
and  e.getCause().printStackTrace(System.out);

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.out);... printed the following:

java.lang.Exception: Four
      at test.ExceptionTest.main(ExceptionTest.java:22)
Caused by: java.lang.Exception: Three
      at test.ExceptionTest.main(ExceptionTest.java:20)


e.getCause().printStackTrace(System.out);... for the exact same exception printed the following:
java.lang.Exception: Three
      at test.ExceptionTest.main(ExceptionTest.java:20)