But there are places where you might suffer an exception regardless. What can you do?
Prevention is better than cure
salvin francis wrote:
I would think it is better style to declare only checked exceptions in the throws clause, but to describe all exceptions known about in the documentation comments. Then the user knows what to expect.
Stephan van Hulst wrote:. . . Unchecked exceptions may be declared, checked exceptions must be declared. . . .
And I was lying shamelessly. I shall leave it as an exercise to the reader to predict what exception might be thrown, and to suggest a simple solution that will reliably prevent that from happening.
A few minutes ago, I wrote:. . . Now, we have a nice robust method which cannot go wrong as long as the users follow the instructions in the documentation comments . . .
salvin francis wrote:A small variation of the bubble up example:
It's a bit more verbose, but each method catches it, reports it (alongside some more info) and then bubbles it up.
Campbell Ritchie wrote:Yes, you take an empty kettle to the tap, ask, “tea, coffee, or fruit juice,” and on the answer, “fruit juice, please,” you fill the kettle with 0 of water. Boom: division by 0.