Solved

chaining exceptions

Posted on 2014-02-04
4
273 Views
Last Modified: 2014-02-13
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
0
Comment
Question by:gudii9
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 400 total points
ID: 39833928
I'll explain first paragraph of code...hopefully that will make others clear.

		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

Step 1 ... println executes..."*** no chaining example"
Step 2 ... enter first try catch block
Step 3 ... enter second try catch block
Step 4 ... we throw exception with line "throw new Exeception("One");
Step 5 ... control pass to the inner catch.
Step 6 ... within the innner catch we execute another throw... "throw new Exception("Two")"
Step 7 ... since we threw again... but now control is no longer within second try block, so control is passed 'upwards' to the 'outer' try catch... now we execute second Catch.
Step 8 ... the outer catch just prints the stack trace to the console, so that completes that entire paragraph.

Similar process of control... happens with next two paragraphs.
0
 
LVL 6

Assisted Solution

by:Mahesh Bhutkar
Mahesh Bhutkar earned 100 total points
ID: 39834934
In short the control of exception moves from inner try/catch block to outer try/catch block. And that way chaining is happening. :)
0
 
LVL 7

Author Comment

by:gudii9
ID: 39843192
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
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39843227
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)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 56
Problem to start Neon 20 83
Java Timer (static) 9 36
session migration servlets 2 24
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now