views:

65

answers:

6

Hi,

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

+1  A: 

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;
  }
}
tangens
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.
BalusC
Thanks, but all the exceptions thrown in this method are IOExceptions... (BTW: Congratulations with your 10,000 points!)
Martijn Courteaux
10,000 have to wait till tomorrow. Today the limit of 200 is already reached :-(
tangens
Not if your answer gets accepted... :-)
dty
Not this one, there are too many answers that are better than mine.
tangens
A: 

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

Anon
+2  A: 

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?

andersoj
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.
Mike Deck
A: 

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 !

Colin Hebert
Congrats for the 10k. 46 days; I think that's a new record :)
NullUserException
@NullUserException, Thanks :)
Colin Hebert
+3  A: 

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 */
}
Nikita Rybak
+1  A: 

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