• Post Reply Bookmark Topic Watch Topic
  • New Topic

exceptions: try/catch vs. declare?  RSS feed

 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • 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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you, that helped!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!