Hello,

As a general comment, over time we come to regard specifications like "if these three pre-conditions of the method are all violated, this exception will be thrown; if only these two pre-conditions are violated, this other exception will be thrown" as over specifications and best avoided.

If any of a method's exceptional conditions are true, the caller should be prepared to handle any of the possible exceptions.

HTH,

-Joe

On 6/23/2020 8:03 AM, Roger Riggs wrote:
Hi Raffaello,

Exceptions related to the arguments should be highest priority so they are
not obscured by any more dynamic state related to the stream.

There's no specific order between checks of the arguments,
though in this case the NPE would naturally occur before IOOBE.
There are more complex cases where some arguments are not used
or used conditionally where the checks become dependent on the dynamic state.

$.02, Roger


On 6/23/20 10:34 AM, Raffaello Giulietti wrote:
Hi,

the InputStream.read(byte[], int, int) method [1] can throw
* IOException
* NullPointerException
* IndexOutOfBoundsException

Is there a recommended priority for the conditions associated to the exceptions to be checked? For example, if the arguments are invalid and the stream is already closed, which exception shall be thrown?


Greetings
Raffaello

----

[1] https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/InputStream.html#read(byte%5B%5D,int,int)

Reply via email to