Catch Try has me confused in Android Studio debugging

I'm trying to debug this bit of code in Android Studio. It all seem pretty straight forward but the 'return sb1' in the try won't allow me to break on it (a red dot with an x) and says it's not executable.

    public static String postRecordOnServer(String url,
       ArrayList<NameValuePair> nameValuePairs) {

       String message = "";
       InputStream is = null;
       try {
            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 3000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            int timeoutSocket = 5000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine());
            String sb1 = sb.toString().toUpperCase();
            reader.close();
            is.close();
            return sb1;
        } catch (Exception e) {
            message = e.getMessage();
        }
        return "EXCEPTION:" + message;
    }

And even though it always returns the 'EXCEPTION' calling function gets a NULL result value. The Catch is never fired.

Any suggestions?

Answers


A couple tips you may find useful.-

  • Try directly to debug step by step by adding a breakpoint at the start of the method, just to check what's really happening.

  • Throw your exceptions. Returning a message with the exception is not the way you're supposed to handle issues inside your function. You should add some logic inside your catch block, or just make postRecordOnServer throws Exception, and let the calling function to handle it instead.


Try "File->Invalidate Caches" and restart.

I think I just ran into something similar and it worked for me.

I used some code I found on the internet and it was throwing an exception. Instead of letting the exception bubble up they were putting the details into a custom object and they were passing that around. Well, I didn't like that and I attempted to change it but I had a tremendous amount of difficulty.

There were three classes involved; A, B, and C. My code made a call to A, which made a call to B, which made a call to C, and C was throwing the exception.

The stack trace was making no sense when the exception was thrown after my changes so I decided to make a call to C directly. When the exception was thrown I was still seeing A & B in the trace even though I had bypassed them. Then I completely deleted A & B from my project but they were still showing up in the trace. WTF!!!

After some time pulling my hair out I invalided the caches, restarted, and problem was resolved.


Need Your Help

Use NSUserDefaults with suiteName in Swift

ios swift nsuserdefaults

I am having a problem with NSUserDefaults(suiteName: "group.app-ID").