10

Can anyone help me understand what is going wrong?

unreported exception org.json.JSONException; must be caught or declared to be thrown
    jsonObj = new JSONObject("{\"count\":3939,\"has_more\":true,\"map_location\":{\"lat\":0.60996950000000183,\"lon\":-27.568517000000003,\"panoramio_zoom\":16},\"photos\":[{\"height\":375,}]}"); //creates the JSON object from the jsonString, for parsing
              ^

1 error

I'm using org.json, and I think I have everything set up correctly. I'm trying to create a JSONObject using the constructor in org.json that takes a source string, and I keep getting this exception. I'm not sure what is wrong with the string that I am sending in. Thanks

1
  • 2
    You need to read up on what checked exceptions are in Java - it will clear up this situation for you. Note that this is a compile error, which means it has nothing to do with the string you're passing in. Commented Dec 28, 2013 at 7:00

2 Answers 2

18

Catch your Exception by creating try and catch:

try {
    JSONObject jsonObj = new JSONObject("{\"count\":3939,\"has_more\":true,\"map_location\":{\"lat\":0.60996950000000183,\"lon\":-27.568517000000003,\"panoramio_zoom\":16},\"photos\":[{\"height\":375,}]}");          
    System.out.println(jsonObj);
} catch (JSONException e) {
    //some exception handler code.
}  

Or either throws your exception to caller method:

public String yourMethod(String jsonString) throws JSONException  
Sign up to request clarification or add additional context in comments.

Comments

2

constructor declares to throw org.json.JSONException so you must handle it (catch & handle or rethrow to let caller handle it)

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.