Solved

chaining exceptions

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 63
reverse digits of a number using for loop 5 41
Windows 10 IE Certificate Issue 10 45
Configure a Bean in an XML file 4 32
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

777 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