1

I'm trying to parse a simple JSON string

    try {
        String candyJson = "{\"candies\":[ {\"name\":\"Jelly Beans\", \"count\":10}, {\"name\":\"Butterscotch\", \"count\":6}]}";
        JSONObject candiesJSONobject = new JSONObject(candyJson);
        JSONArray candiesJSONarray = candiesJSONobject.getJSONArray("candies");
        Log.v("JSONObject", candiesJSONarray.getJSONObject(0).getString("name"));
    } catch (JSONException e){
        Log.e("MYAPP", e.toString());
    }

The code works fine in this state without catching any exception and prints JSONObject name in the Android Log.

However when I don't try to catch the exception as shown in the following example:

        String candyJson = "{\"candies\":[ {\"name\":\"Jelly Beans\", \"count\":10}, {\"name\":\"Butterscotch\", \"count\":6}]}";
        JSONObject candiesJSONobject = new JSONObject(candyJson);
        JSONArray candiesJSONarray = candiesJSONobject.getJSONArray("candies");
        Log.v("JSONObject", candiesJSONarray.getJSONObject(0).getString("name"));

Android Studio gives me unhandled exception error on all JSON methods. Is it necessary to catch JSONException when parsing a JSON or am I doing something wrong?

3
  • 1
    Read Exception handling first. Commented Dec 1, 2016 at 12:08
  • your json is not well formed . Invalid json . jsonviewer.stack.hu Commented Dec 1, 2016 at 12:29
  • The JSON String is working fine. I'm using backslashes for escaping quotation marks. Commented Dec 1, 2016 at 12:38

4 Answers 4

3

This is a Java feature actually :-) Please read more about it here.

The idea is that - if a method states that it will throw an (non-Runtime) Exception, all the calls of that method are required to catch this exception, just in case.

It does not mean that you are getting this exception in your code, you can only see that when you actually run it. But Java requires you to be prepared for a situation where such exception is thrown.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! That link is very helpful!
If it answers your question, please mark the answer as correct :-)
I have to wait 4 minutes to accept the answer. Will do it asap.
Well here the problem is that the buildin version of org.json is outdated. JSONException should not be Checked Exception. There is no added value in having to try catch everything, it make the code unreadable. In newer versions of org.json (stleary/JSON-java) JSONException extends RuntimeException. Unfortunately due to naming conflict we can't use it.
1

Well since you're working with the org.json... json objects, yes most of their methods do throw exceptions that you must catch and handle.

However if you don't want to handle each exception on it's own i suggest you create a json utils class that will handle those things for you.

For example for the JSONObject constructor you can make your own method like so

    public static JSONObject createObjectFromString(String objectString) {
    try {
        return new JSONObject(objectString);
    } catch (JSONException e) {
         Log.e("MYAPP", e.toString());
    }
}

and just reuse this method when you want to create a new json object.

Comments

0

Yes actually if any method is throwing Exception you need to catch that Exception. This is called as Checked Exceptions or Compile Time Exceptions.

In your case methods like

JsonArray getJsonArray(String name)

or

JsonObject getJsonObject(String name)

check here http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html#getJsonArray-java.lang.String-

are throwing ClassCastException So you either catch it or throw the exception. Throwing Exception will lead to crash the app, So better Catch it.

Comments

0

If any method throws checked Exception, then caller can either handle this exception by catching it or can re throw it by declaring another throws clause in method declaration.

This is the reason Android Studio is showing unhandled exception error.

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.