Solved

chaining exceptions

Posted on 2014-02-04
4
272 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

18 Experts available now in Live!

Get 1:1 Help Now