Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Wrapping checked exceptions in a RuntimeException

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Simple question here: is wrapping a checked exception in a RuntimeException, like:



considered good practice? The exception originates in your DAO (lowest level), can't be recovered from along the way, and only results in an error message for the user (highest level).

I don't want to put "throws DaoException" in every one of my method declarations, but the exception still has to propagate to the user.

Any ideas?

Thanks!
 
Author
Posts: 3443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a web application or a stand alone java application? You can capture the RuntimeException at the top level where you can log it with full stack trace, etc. Spring and Hibernate 3 uses runtime exceptions.
[ May 14, 2008: Message edited by: arulk pillai ]
 
Marshal
Posts: 66463
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in "Thinking in Java" by Bruce Eckel, you find he changes his mind between the 3rd and 4th editions. He now recommends Exception chaining just like what you have shown. Might be better to create our own Exception so you can write

catch (JanVansException jve) . . .
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess your statement 'I don't want to put "throws DaoException" in every one of my method declarations, but the exception still has to propagate to the user.' answers it. This sort of chaining will be useful in cases where we cant recover from exceptions but still would like to propagate it without the hassle of having the caller methods having to declare throws clauses for the checked exception. Maybe we can use our own custom runtime exceptions if needed, as stated in one of the posts.
 
Run away! Run away! Here, take this tiny ad with you:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!