0

I have run and debugged this java code in Android and I have noticed that the return true has no effect because it act as a break and then the return false is finally executed. I have used the Android Studio and its Step Over function for debugging.

protected Boolean doInBackground(Void... params) {
   // Cancel discovery because it will slow down the connection
   mAdapter.cancelDiscovery();
   // Start connection attempt
   for(int i=0;i<10;i++) {
      try {
         // Connect the device through the socket. This will block
         // until it succeeds or throws an exception
         mmSocket.connect();
         // connection successful [it should return true]
         return true;
      }
      catch (IOException connectException) {
         // Unable to connect; close the socket and get out
         try { mmSocket.close(); }
         catch (IOException closeException) {  }
      }
   }
   // connection failed 10 times [but it always returns false]
   return false;
}

UPDATE:

Like EJP and others say, return true does not act as a break. The doInBackground function runs as expected. The problem was that, the AsyncTask was always cancelled just after the execution of return true because of a external code block. I'm sorry about that.

This post can be closed. Thank you.

Ivan Meyer
  • 41
  • 5
  • If you write a return in For it always acts as a break. plus even if some exception occurs in your connection you are just catching it and not returning after that. Why do you want to put your connect statement in a for loop. even if you want to ensure that it keeps on trying until you connect, do it in a do while loop and check in while if the connection is established successfully. – Kapil G Jul 30 '17 at 05:18
  • it is not very good programming because I use the return statement inside a loop and this would cause garbage collection to malfunction. – Fady Saad Jul 30 '17 at 05:20
  • @FadySaad No, it wouldn't cause garbage collection to malfunction. Can't imagine where you got that idea. – user207421 Jul 30 '17 at 06:26

4 Answers4

0

Are you sure there is no exception? from your code, it will returns true if there is not problem with .connect method, try to log your exception to check whatever the connection is successful.

0

If there is an exception occuring, the return true will not take effect. Why false returned, because the exception was caught silently.

I can give you a sample code here,

private static void connect() throws Exception
{
    throw new Exception();
}

private static Boolean doInBack() {
    for (int i = 0; i < 10; i++) {
        try {
            if (i % 7 != 0 || i == 0)
                continue;
            connect();
            return true;
        } catch (Exception e) {
            System.out.println("Exception");

        }
    }

    return false;
}
public static void main(String[] args)
{
    System.out.print(doInBack());

}

A reference about the try-catch-finally-return execution order.

caisil
  • 363
  • 2
  • 14
0

I have run and debugged this java code in Android and I have noticed that the return true has no effect

Only if there was an exception. Ten exceptions.

because it act as a break

No it doesn't.

and then the return false is finally executed.

No it doesn't. The only way the final return can be reached is if there were ten exceptions.

Ergo there was an exception. Ten exceptions. The first one was a ConnectException, and the ones after that all related to your invalid attempt to reconnect the same socket instead of creating a new one first.

Trace the exceptions and see for yourself.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Similar to what EJP said, your code inside the try-catch is failing 10x, not because the return true is not behaving the way you expected it. And when it fails 10x, as it should, you end up at return false.

You need to log your connectException and closeException, at least with something like e.printStackTrace.

Also, don't let multiple return's short sight you. Try it like this:

protected Boolean doInBackground(Void... params) {
   ...
   boolean success = false;
   for (int i = 0; i < 10; i++) {
      try {
         ...
         mmSocket.connect();
         success = true;
      } catch (IOException connectException) {
         ...
      }
   }
   return success;
}

if connect() succeeds, then success = true, otherwise success will stay false. (now there's no doubt over your confusion on why a return is acting like a break)

As a side note, yes, without more context here, you may need to consider that if the first connect() fails, then it is possible that all subsequent connects will too.

TWL
  • 6,228
  • 29
  • 65