richard
richard

Reputation: 854

return statement ignored while debugging

i wrote a method that creates a JSONObject, that used to work. suddenly i behaves very strange. with the debugger i can reach the line that returns the created JSONObject, but it always jumps to the last line returning null, without going insid the catch block. the debugger showed me that the created object is a valid JSONObject.

import org.json.JSONObject;

...

JSONObject returnJson = new JSONObject();
    try {
        returnJson.put("event", event);
        returnJson.put("list", jsonArray);
        returnJson.put("type", "list");
        returnJson.put("container", container);
        Log.d(TAG, "created");
        Log.d(TAG, returnJson.toString());
        return returnJson;
    } catch (JSONException e) {
        Log.e(TAG, e.getMessage(), e);
    }
    Log.d(TAG, "null returned"); // never called in the non-debugging mode
    return returnJson;

Upvotes: 1

Views: 776

Answers (1)

Attacktive
Attacktive

Reputation: 615

I guess it didn't hit the last return returnJson; but the former one. I mean the code inside try block did not throw an exception.

I've witnessed the debugger break on return statements that isn't actually hit under some condition I am not well aware of.

I just wrote example code to reproduce:

private static String method() {
    try {
        Class clazz = AnyClass.class;
        return clazz.getName();     // place a break point #1
    } catch(Exception ignored) { }

    String result = "An exception caught.";
    return result;                  // place another break point #2
}

It will surely break at #1 and then "stop" at #2, too. This method actually returns "package.name.AnyClass".

Upvotes: 1

Related Questions