1

Is there a way in Java to build a JSONObject without having to deal with an exception? Currently I'm using the default constructor, which however forces me to put try/catch blocks in the code. Since in the rest of the code I'm using the "opt" version of get, checking if the return value is null, is there a way to build the object in the same way? (that is, some kind of constructor that returns null if it can't build the json from a string).

Example:

         try {
               JSONObject temp = new JSONObject(someString);
         } catch (JSONException e) {
               e.printStackTrace();
         }

What I would like to do:

JSONObject temp = ??????(someString);
if(temp != null) {...}
5
  • Why don't you put return of null in catch block? Commented Jun 30, 2015 at 9:23
  • Because I want to avoid the catch block altogether. Commented Jun 30, 2015 at 9:25
  • 2
    A decent JSON parser will never fail silently and return null is the JSON is not parsable. It will throw an exception. If you want to fail silently and return null, then create your own utility method that does that (by parsing and catching the exception). But I would really not do that if I were you. Dealing with obscure NullPointerExceptions is much worse than dealing with clean JSONException, indicating what and where the problem is. You can throw a runtime exception if you want, which would not force the caller to deal with the exception, but would still signal a bug in a clear way. Commented Jun 30, 2015 at 9:30
  • Thanks for the suggestion of a runtime exception, I'll probably resort to that in the end. Commented Jun 30, 2015 at 9:46
  • Hey, you know what JSON manipulator allows you to check properties without throwing an exception? JavaScript. You know, the originator for JavaScript Object Notation. In that if there's illegal characters in the JSON structure then sure, it'll fail. But it's perfectly possible to check if a property exists without having to use try and catch. Having exceptions thrown for every single manipulation, even for just getting objects is ridiculous. It adds massive code bloat and makes the program more difficult to read. Honestly, the amount of unnecessary boilerplate code is a problem with Java. Commented May 13, 2016 at 19:21

1 Answer 1

3

You need to create the method manually, as any parser will most probably throw one or the other Exception in case of any discrepancy.Try something like this:-

 public JSONObject verifyJSON(String inputString){
        JSONObject temp;
        try {
            temp = new JSONObject(inputString);
      } catch (JSONException e) {
          temp = null;
            e.printStackTrace();

      }
        return temp;
    }

Then you can do :-

JSONObject temp = verifyJSON(someString);
if(temp != null) {...}
Sign up to request clarification or add additional context in comments.

2 Comments

I understand that this is the most I could do, together with the suggestions of JB Nizet of using a runtime exception. I guess it's easier to deal with exceptions though.
yeah sure @flower_green. YOu can handle runtime Exceptions also & can perform whatever operation you want to do. Also handing the SuperClass Exception type might also be a good option if any other error occurs except RuntimeException

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.