Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Seriously Good Software: Catching exceptions

 
Ranch Hand
Posts: 80
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this pop quiz:  What’s wrong with throwing a checked exception as a penalty?. It brought a different question to my mind.
What's wrong about catching/handling exceptions and not allowing it to propagate?
What do you think? Should we always allow exception to propagate?
Do you give any advice in your book about this?

Thanks.
 
Author
Posts: 9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions should be caught at the level where the appropriate corrective action can be taken.

Corollary: an exception should never be caught by the method that threw it. If it threw it, by definition it doesn't know how to fix it.

If no corrective action is possible, catch it at the level when such impossibility can be ascertained (probably close to your "main" or entry point)
and log it and/or inform the user, as appropriate, before exiting.

In the book I work a lot with exceptions in chapter 5 in the context of reliability,
focusing on when, why, and what type of exceptions to throw.

Marco
 
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question.

Jorge Ruiz-Aquino wrote:. . . Should we always allow exception to propagate?  . . .

No. Most of the time there is something you can do to deal with the exception, and as MF has told you, work out where you can best correct the problem.
 
Jorge Ruiz-Aquino
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are great advises.
Thank you Marco Faella and Campbell Ritchie.
 
Did you just should on me? You should read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!