'Java exception handling: using Exception (example: catch (Exception e)) vs specific exception (example: catch (IOException e))
Is there a reason to catch specific exceptions vs just using the Exception class if I don't have special cases/logging for the specific rule?
I current have
try {
//do IO stuff here and other things
} catch (IOException) {
system.out.println("Im here");
}
Since I don't have special logging required for the error, I don't see a reason why I should use IOException vs just using Exception which can capture al exceptions.
Thank you!
Solution 1:[1]
You do this because you might want to have a different behavior for an IOException (for example, asking the user to try again) versus an OutOfMemoryException, for which you might free up resources, versus some other kind of exception that means the program has to quit.
If you're handling all those cases the same way, then you can use a broader exception class, like Exception or Throwable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Gustafson |
