• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

exceptions: try/catch vs. declare?

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if a method needs to handle an exception,
I can choose whether to try/catch it or to just declare it.
which are the criteria for choosing one over the other?

I know the theoretical rules about exception handling but have no feeling yet
which to use when in "real programming life".

Any tips from you experienced folks?
Thanks!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might like to read a bit of a past JavaRanch conversation on the topic of exceptions as well as http://c2.com/cgi/wiki?IlluminateTheMainline.

And while it might seem a bit off-topic, for me, the ideas explained in a recent thread on the Null Object Pattern fit in well with a few of the questions that arise when considering what you're pondering.
[ June 13, 2004: Message edited by: Dirk Schreckmann ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fundamental rule is to catch when you can, and throw if you must. The purpose of an exception is to send a message about an unexpected error condition. If a routine catches an exception, then it is claiming that it knows best how to handle the problem. Contrariwise, to declare a thrown exception is to say that you don't know how to handle the problem in that routine.

So, for example, FileReader's constructor throws an exception if a file isn't found, because it has no idea how your application wants to handle the problem of a missing file. If you use FileReader to write a routine readInFile(), then it should simply declare the IOException, since it can't read in a missing file.


On the other hand, if you write an "initialize()" method for an application, that method shouldn't throw an exception if an optional resource file isn't found; it know what to do about the problem itself, and so should catch the exception thrown by FileReader rather than declare it.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"if a method needs to handle an exception" - are we talking about checked exceptions here? Because you never actually need to handle an unchecked exception; to put it another way, it's generally best to prevent unchecked exceptions by making sure you don't pass illegal arguments into functions.

If a function declares a checked exception, the function is probably part of a library, and the exception is probably intended to be handled locally by the method calling the library. So yes, you should handle it locally, unless for some reason the design of your application is such that it's intended that the exception be handled in a higher layer of code.
 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, that helped!
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic