6

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

6 Answers 6

9

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 */ } 
Sign up to request clarification or add additional context in comments.

Comments

3

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?

1 Comment

Agreed. It's rarely a good idea to catch Exception or RuntimeException. Generally you want to catch more specific subclasses. And please don't ever EVER catch Throwable or Error.
2

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; } } 

4 Comments

He just wanted to handle IOException s other than the manually thrown one. Btw, I'd add another catch block rather than doing an instanceof in this particular case.
Thanks, but all the exceptions thrown in this method are IOExceptions... (BTW: Congratulations with your 10,000 points!)
10,000 have to wait till tomorrow. Today the limit of 200 is already reached :-(
Not this one, there are too many answers that are better than mine.
2

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 */ } } 

Comments

0

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).

Comments

0

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 !

1 Comment

Congrats for the 10k. 46 days; I think that's a new record :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.