• Post Reply Bookmark Topic Watch Topic
  • New Topic

Chained Exceptions, Am I doing it correct?  RSS feed

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am reading about Chained Exceptions and this is what I understood from it. Can someone tell me if I am in the right track?/

Chained Exceptions are Exceptions which are caught in a catch block which in turn lead to throwing new Exceptions if we have a throw newException in the catch block. We can use the constructor Throwable(String, Throwable)
to display both the exceptions. String object is the new Exception and Throwable object is the actual exception which is responsible for generating the new exception.

Below is the code I wrote:





Am I correct? Please correct me if I am wrong.

Thanks.
[ November 13, 2008: Message edited by: Arjun Reddy ]
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String object is the new Exception and Throwable object is the actual exception which is responsible for generating the new exception.


How can String Object be a new Exception? It is the informational message for the user that we pass through the constructor.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um... yes. Other than that, everything else is correct right?/
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
[ November 14, 2008: Message edited by: Patricia Samuel ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Patricia Samuel:
Yes.

[ November 14, 2008: Message edited by: Patricia Samuel ]




Thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate a lot of Java 1.4 exceptions. Several, including IOException and SQLException, didn't have the throwable in their constructors. So you had to use a workaround:

Fortunately, that got solved in Java 6.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am confused. I do not understand the concept of catching an exception and throwing a new exception from the catch block again. Why would you want to throw a new exception?

Thanks.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are implementing a method that cannot throw the actual exception, but can throw a different exception. Especially when implementing interfaces you will run into this problem.

An example of mine. I have a class called DocumentWriter, which is a java.io.Writer that writes to a javax.swing.text.Document.

Document has a method for adding text called insertString. However, this can throw a checked BadLocationException. The write method cannot throw this but only IOException (and subclasses). So I wrap it:
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Because you are implementing a method that cannot throw the actual exception, but can throw a different exception. Especially when implementing interfaces you will run into this problem.

An example of mine. I have a class called DocumentWriter, which is a java.io.Writer that writes to a javax.swing.text.Document.

Document has a method for adding text called insertString. However, this can throw a checked BadLocationException. The write method cannot throw this but only IOException (and subclasses). So I wrap it:


Hi Rob,

Thanks for your reply but I still have some questions in my mind. 1).Why can't the write method just throw the BadLocationException and 2).why do you need to throw it when we have already caught it?/

Thanks.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the superclass, Writer, does not allow for BadLocationException, only IOException and its subclasses.

That's the most used reason for wrapping a throwable in another throwable - because the interface or superclass is limiting the number of exception types you can throw.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!