Java: How to throw an Exception to the method caller inside a try catch body?
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 poss开发者_如何学编程ible 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 */
}
I think I'm confused by the specifics of your example -- why are you making the broad
} catch (Exception e) {
If that overly-generic catch clause wasn't there, your problem would go away.
Did I misunderstand?
You can check if your exception is an instance of IOException
and if it is, rethrow it.
catch( Exception e ) {
if( e instanceof IOException ) {
throw (IOException)e;
}
}
You can catch it, and re-throw it, but the correct solution should be to be more selective of what you are catching in your catch statement....
For example. In the code below, I am catching a FileNotFoundException, so the IOException is thrown back to the calling method.
public static void foo(String param) throws IOException {
try {
// some IOoperations
if (param.isEmpty())
{
throw new IOException("param is empty");
}
// some other IOoperations
} catch (FileNotFoundException e) {
/* handle some possible errors of of the IOoperations */
}
}
I would create a subclass of IOException
. This will let you use an instanceof
check and rethrow.
And there are very few cases where I use catch (Exception)
. It's almost always better to catch specific exceptions. The only reason not to is when you want to aggregate exceptions and rethrow (reflection operations are a common place to do this).
If IOExceptions are meant for the foo()
caller, you can do 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 (IOException e) {
throw e;
} catch (Exception e) {
/* handle some possible errors of of the IOoperations */
}
}
If your exception is the only one really needed to be rethrown, then create a new Subtype of IOException name it MyException
an try to catch MyException
to rethrow it.
Any pokemon exception handling is very ugly !
精彩评论