Almost every time I think I have no use for an exception, one occurs to me after a while. Usually.
If you honestly can't think of anything worthwhile to do with an exception instance, it's time to consider the possibility that you have not identified the correct spot in the program flow at which to handle the exception.
Another possibility, perhaps more likely with exceptions you've written yourself, is that your exception does not provide what you need to recover from the exceptional condition.
And hopefully the least likely possibility is that you're abusing the exception system to do something that ought to be done with another flow control structure, like an if or while block.
I've wondered about that in my Algebraic Integer Calculator project, in which you can (or will be able to) do things like inquire what's the greatest common divisor (GCD) of 8 and 3√2. The answer is of course √2, but the computer needs some kind of algorithm, like the Euclidean algorithm, to figure that out.
So my program divides 8 by 3√2. That causes NotDivisibleException
to be thrown. That exception should provide at least one rounding function by which to round 8/3√2 to an algebraic integer.
Other times, though, I just want to know whether one algebraic integer is divisible by another. For example,
public static boolean isDivisible(AlgebraicInteger a, AlgebraicInteger b) {
try {
a.divides(b);
return true;
} catch (NotDivisibleException nde) {
return false;
} catch (AlgebraicDegreeOverflowException adoe) {
return false;
}
}
As you can imagine, this gives me an unused exception warning. It's the main reason why I wonder if the NotDivisibleException
is an abuse of the exception system.
What's the alternative here? I could rewrite the division function in isDivisible
so that it gives me the true or false without triggering any exceptions.
Therefore, this one unused exception warning is preferable to duplicating the division function almost verbatim.
As for AlgebraicDegreeOverflowException
, I decided that's a runtime exception that should probably be caught by the caller, not by isDivisible
. The adoe
clause is therefore stricken.
So this leaves me with one unused exception warning. I think maybe the best I can do with it is limit it to a single occurrence, by encapsulating it in a function rather than repeating it each time I need to do a simple divisibility test.
In the Scala version of this program, I can use "_
" to indicate I don't actually need the exception object. Or I could even overload the modulo operator so that it can handle AlgebraicInteger
operands, or define an implicit conversion, etc.
But that's Scala. In Java, there are only three options:
- Use the exception in some way
- Suppress the warning
- Ignore the warning
The last two are kind of the same, though.
I am aware that this is a question from eight years ago. I'm curious to know what you decided to do in the case of the FileNotFoundException
.
In your shoes, I think I would have decided to at least log the exception message, after retrieving it with the getMessage()
function. There might be some extra context you couldn't put on here which might suggest a different solution.