4

I'm working on an Android application that uses sockets. I have a function called initializeStreams() which opens the socket and attempts a connection. This function throws a ConnectException if the connection could not be established. But for some reason, in the code that calls initializeStreams(), which has a catch block for ConnectException, the log prints out its own stack trace for the exception instead of going to the catch block. The catch block is never reached at all, even though the exact exception is being thrown. Here's the code:

The try block:

try {
        initializeStreams();

            /* other code */

    } catch (ConnectException e) {
        Log.i(TAG, "caught connect exception");

    }

initializeStreams():

    public void initializeStreams() throws ConnectException {
    try {
        Log.i(TAG, "Attempting to connect");

        requestSocket = new Socket(SERVER_ADDR, PORT);

                    /* other code */


    } catch (IOException e) {
        e.printStackTrace();
    }

I can't figure this out, so any help would be much appreciated.

aakbari1024
  • 167
  • 1
  • 3
  • 9
  • We cannot figure out why that's happening since the only socket operation that you posted in your code does not throw a ConnectException. Post the rest of the code. – Th0rndike Jul 03 '12 at 15:16
  • Yea. The Log right before the socket initialization is printed, then the ConnectException stack trace is printed, instead of the Log that's in the catch block. initializeStreams() is the only function that throws ConnectException and the first code snippet is the only place where it's called. – aakbari1024 Jul 03 '12 at 15:19
  • what do you mean? That socket operation is the one that throws the exception, it says so in the stack trace, and it's the only socket operation in my program at the moment. – aakbari1024 Jul 03 '12 at 15:22
  • I don't see any method that throws ConnectException here: http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html – Th0rndike Jul 03 '12 at 15:26
  • @Th0rndike ConnectException extends SocketException. – user207421 Jul 03 '12 at 15:45

2 Answers2

3

You need to chain your Exception throwing it in the catch block. Try the following:

public void initializeStreams() throws ConnectException {
try {
    Log.i(TAG, "Attempting to connect");

    requestSocket = new Socket(SERVER_ADDR, PORT);

                /* other code */


} catch(ConnectException e){
    throw e;
}

catch (IOException e) {
    e.printStackTrace();
}
Arun George
  • 18,352
  • 4
  • 28
  • 28
  • Strange, because it doesn't even compile in the pre-edit form. The edited version of initializeStreams() contains a futile catch/throw sequence that would be better omitted completely. – user207421 Jul 03 '12 at 15:41
  • @EJP yes you are right. The edited version does have a futile try/catch block. I have corrected my post. Thanks for pointing that error. – Arun George Jul 03 '12 at 15:46
  • The first version still doesn't compile: the catches are in the wrong order. The edited version should *catch* `ConnectException` specifically instead of using an `instanceof` test, and `initializeStreams()` only needs to throw `IOException`, not `Exception`: the latter is poor practice. – user207421 Jul 04 '12 at 00:11
  • I did some research and found that using `instanceOf` for `exceptions` is not a recommended practice. I have removed that completely. But the `catch` block are in the right order and should work if the code inside the `try` does throw a `ConnectException` or `IOException`. – Arun George Jul 04 '12 at 04:38
2

ConnectException extends SocketException which in turn extends IOException, so the catch for IOException in initializeStreams() catches the ConnectException. I would just remove that try/catch block altogether: there's not much point in returning cleanly from this method without a connection.

user207421
  • 305,947
  • 44
  • 307
  • 483