Solved

chaining exceptions

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
table example 4 32
java jdbc batch example 8 41
Select statement in @Query (JPA Repository) - SpringMVC 2 19
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

820 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