Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

getCause() method

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




can anybody tell whether it is possible to add cause in this class. Right now getCause() is fetching null.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Suaresh,

Please Use Code tags when posting on the forum, it makes your post easier to read.


On your query, if you read the javadoc for the Throwable class, you will see that the getCause() method returns null if the cause is nonexistent or unknown.

I recommend that you override the initCause() method from the Throwable class in your WrongLengthException class and return an appropriate Throwable.



Kondwani.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't bother doing that.

getCause() is used for chained exceptions, where you have one wrapped inside another - it represents the exception that was the underlying cause. In this case there is no underlying cause, so it correctly returns null.

What would you want it to return? If you want a description of what has gone wrong, that's what the message is for.
 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Exception has the cause as null. Hence it is printing null. Notice the constructor in the Throwable interface class that takes a Throwable and the one that takes a message and a Throwable. If you do not construct your Exception object by passing it a Throwable argument, the cause will be null.

getCause method is generally helpful when you wrap one exception into another. Like for example-



The calling code can have


This should now print something like
Exception1 : and the reason is null.

If you want the message also, in the constructor parameters, you need to provide both - the message ( String ) and the Throwable and need to pass both of them as parameter while throwing the new Exception.

However, it is not advisable to wrap one exception into another unless there is a good reason to do so.

Note that you can also make use of the initCause method but for your case I don't think that is necessary.
 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getCause() is used with chained Exceptions as Matthew said. If you want to print the cause modify your main method like below:



This will print the actual cause of Exception
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoever has given a thumbs up to my post, please note that Throwable is a class, not an interface.

I have edited my response with a strikethrough.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic