I have an exception (MalformedDataException) which extends IOException. Somewhere in the code it is thrown and it is temporarily 'caught' by the main class, like this:
public static void main(String[] args) throws MalformedDataException { ... }
But what is an input/output exception exactly? When does it have to be thrown? Should it be extended?
The Oracle docs do not explain anything about it.
An input / output exception is something that has messed up normal input and output operations. It is purposefully vague to cover a large variety of scenarios, including reading from a file that was closed, to reading from a file that was removed, to reading from a socket that was closed, to just about anything that could go wrong reading or writing to anything.
If you write code that can recover from a failure (like a network connection that attempts to reconnect on a bad read), you should catch the IOException and perform the "recovery" procedure to continue processing. If you don't actually recover from a failure, you should (probably) just allow the IOException to rise up to the next level by marking the method throws IOException.
Capturing IOExceptions and doing nothing with them is a recipe for really bad bugs that are hard to debug. You have been warned. Don't do it.
You should not extend an IOException unless you have a rare situation where your "new" exception to be thrown is subject to failures that stem from input / output instability. In the case of MalformedDataException, the reason it is thrown is because you read something from the input that doesn't even look like what you expected.
Since input and output typically is subject to line interference, there is some controversy as to whether an IOException is appropriate for malformed data. I personally lean on the "not appropriate" side of things, as it will tend to lead people to questioning the integrity of the line, instead of the integrity of the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With