• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Chained Exceptions, Am I doing it correct?

 
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 ]
 
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
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
this is supposed to be a surprise, but it smells like a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic