Reputation: 963
So I have this one question. Lets say we have classes: Main
, Info
, Cats
, Food
Now, lets say that in main we create new object Info
. In Info
object we are saving list of Cats
that have been created. Cats
are being created and stored in Info
class and Food
is being created and stored in Cats
class. Now lets say, that in Main
class, I want to get specific Food
object, which is stored in Cats
class. So, in order to do so we do the following:
Info.getFood(name)
. Then in Info's
getFood
method we say Cats.getFood(name)
. Finally, in Cats
class we have method getFood
, in which we try to find Food object by its field "name". If we are unable to find such an element, we throw NoSuchElement
exception rather than return an object. Here is my question:
If we throw exception in Cats
class getFood
method, should we catch that exception in Main
class (where our interface is), in Info
class (which is our system class) or in both of them?
Upvotes: 0
Views: 101
Reputation: 8185
Generally speaking, inside a method, if you can do something with the Exception
being thrown (log an error, show an error message, make a different decision in your code, etc), then you should catch it. Otherwise, just throw it to the calling method.
As with many other coding practices, it all boils down to what you and your team agree on.
A concrete example which isn't related to your code, but which will show you how the decision process can be made. Assume the following code:
public MyConfiguration loadConfiguration () throws ConfigurationException {
MyConfiguration config = null;
try {
readConfigurationFromFile ();
// Parse configuration string
} catch (IOException ioex) {
throw new ConfigurationException (ioex);
}
return config;
}
private String readConfigurationFromFile () throws IOException {
String configuration = "";
// Read a file on disk, append data to the string.
return configuration;
}
In readConfigurationFromFile ()
, if an exception occurs while reading the file, you'll get an IOException
. At this point in the code, there's no real action you can take, since this method only reads the configuration file, appends the data to a String
, then returns it.
In loadConfiguration ()
, you can surround the call to readConfigurationFromFile ()
with a try/catch
, and throw a more generic exception (ConfigurationException
). Again, at this point, there's nothing you can do with the exception, except wrap it in a new exception which adds more context information to the original exception that was thrown.
Now assume that there's two flavors of your software: a GUI version, and a command-line version. If you are running the GUI flavor, then the method calling loadConfiguration
could decide to show an error message to the user whenever a ConfigurationException
is being thrown, so that the user knows that something happened. If you are running the command-line version, then maybe it would be more logical to add an entry to some error log with the exception that was caught.
Upvotes: 2
Reputation: 2733
It depends a lot on what you want to do after throwing that exception.
Say for instance that if all you want is to return any food object from any cat (and as you said 'Info' stores lots of cats) then you might have a catch in Info where you catch the NoSuchElement exception and then create some logic that moves onto the next Cat in Info to get its food! Finally if you exhaust all the 'Cats' in Info with no food found, you can throw another exception inside Info that you catch in Main that lets main know, "There's no food".
Again that's just an example. As people have said, it's not a "Always do this..." kind of answer. It depends greatly on what you need to do when handling that exception
Upvotes: 0
Reputation: 1538
The following site says "Most of the developers are embarrassed when they have to choose between the two options. This type of decision should not be taken at development time. If you are a development team, it should be discussed between all the developers in order to have a common exception handling policy."
http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions
Upvotes: 0