When I have a method like this:
public static void foo(String param) throws IOException
{
try
{
// some IOoperations
if (param.isEmpty())
{
throw new IOException("param is empty");
}
// some other IOoperations
} catch (Exception e) {
/* handle some possible errors of of the IOoperations */
}
}
And when the IOException ("param is empty") is thrown, it is catched by the try-catch
in that body. But this exception is meant for the caller of this method. How can I do this properly? Is there something "pure-Java" to do this or do I have to create an other type of Exception which is not an instance of IOException to avoid the try-catch body will handle it?
I know you would suggest to use a IllegalArgumentException
in this case. But this is a simplified example of my situation. In fact the Exception I throw is an IOException.
Thanks
Making your own custom subclass of IOException
might be a good idea. Not only to solve this problem, but sometimes it's a bit 'user-friendlier' for your API users.
Then you could ignore it in catch block (rethrow it immediately)
} catch (FooIOException e) {
throw e;
} catch (Exception e) {
/* handle some possible errors of of the IOoperations */
}
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