Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chained exception

 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is chained exception ???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you wrap an exception that is the root cause in another exception.

The Servlet Spec makes use of it.

the doGet, doPost etc. methods of HttpServlet throw a ServletException.

Imagine code like this.



You chain the SQLException together with the ServletException since you cannot throw it yourself (the signature does not allow this)

ServletException itself has a method called getRootCause() that returns the Exception you chained.
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Beginning Java.
 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we wrap a Checked Exception in a RuntimeException ??
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. If the class does not have a constructor that has a Throwable parameter for the cause, you can always set the cause later:

And please read your private messages regarding an important announcement.

Thank you,

Rob
 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mu gaandimara wrote:

Please check your private messages for an important administrative matter.
 
Ashutosh Meher
Greenhorn
Posts: 3
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chained exception is a feature introduced with JDK 1.4, which allow developers to associate an exception with another exception.
For example, suppose you encounter an ArithmeticException because of a division by zero, however the actual cause of the error is because of an IO error which caused the divisor to the set incorrectly. Chained exception helps us to identify such cases i.e. cause of an exception.

Below is an article you might want to read for further understanding:
http://www.cubearticle.com/articles/core-java/basics/chained-exception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic