4

java.net.ConnectException extends java.net.SocketException

If I do the following, will it cater for both exceptions? ie if I catch a "parent" exception using instanceof, does that include any subclassed exceptions?

catch (Exception e)
{
   if (e instanceof java.net.SocketException)
   {
      System.out.println("You've caught a SocketException, OR a ConnectException");
   }
}

(and for the record, yes I know catching plain Exceptions is bad, just using it for this example ;) )

3
  • 3
    Why not just to try it? (yes, you catch). Commented Nov 11, 2010 at 11:52
  • 1
    It would have taken you less time to just try it than to ask it here... Commented Nov 11, 2010 at 11:57
  • Who says that every question asked on SO is asked by someone sat with an IDE? Don't have a development environment on my iPhone... Commented Nov 11, 2010 at 12:09

5 Answers 5

11

Exceptions are regular classes, so instanceof works fine for them.

But you don't need such a thing. The following achieves the same result:

try {
    throw new ConnectException();
} catch (SocketException e) {
    System.out.println("You've caught a SocketException, OR a ConnectException");
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if you need to do some common code during the catch clause but have to throw another specific exception depending on the exception that was caught? Then instanceof is cleanest right?
or different catch clauses?
4

Yes, it will cater for both. Because ConnectionException IS A SocketException, it also is an instance of it.

Comments

3

Bozho already has given the right answer. I don't know your particular usecase, but you'd rather catch different exceptions:

try {
  ...
}
catch (SocketException ex) {
  System.out.println("You've caught a SocketException, OR a ConnectException");
}
catch (Exception ex) {
  ...
}

Comments

2

I know that it's now a good way but if you want to do custom action in a many places in code you can do something like this: public class ImageIOExecption extends Exception {

Exception ex;

public ImageIOExecption(Exception ex) {
    this.ex = ex;
    doCatch();
 }

private void doCatch() {
    System.err.println(ex.getClass());

    if (ex instanceof java.net.SocketTimeoutException) {
        System.out.println("You've caught a SocketTimeoutException, OR a ConnectException");
    }

    if (ex instanceof java.net.SocketException) {
        System.out.println("You've caught a SocketException, OR a ConnectException");
    }


}
}

 public BufferedImage getBufferedImage() {
        try {
            BufferedImage srcImage = ImageIO.read(is);
            is.close();
            return  srcImage;
        } catch (Exception ex) {
            new ImageIOExecption(ex);
        }
        return null;
    }

Comments

0

Yes, that is how instanceof works. For exceptions it is more common to use something like this if you care about different exceptions. It works because the JVM will work down the list of catch statements in order and execute the first one that matches.

catch(ConnectException e)
{
    //got ConnectException  
}
catch(SocketException e)
{
    /got a SocketException
}
catch(Exception e)
{
   //got some other exception
}

Or below if you dont care about the difference between Connection and Socket Exception

catch(SocketException e)
{
   //got a SocketException or a subclass e.g. ConnectionException
}
catch(Exception e)
{
   //got another type of exception
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.