handle loadJSONObject 400 error gracefully

edited December 2014 in How To...

Hey all,

I'm looking to clean up my output panel by gracefully handling http 400 errors when I try to load an invalid url. I'm using the mapquest geolocation api to lookup latitude and longitude values for location strings. often (so it seems) the location is malformed or results in an invalid url, which is ok for my program, but prints out 10 or so lines to the console, making it difficult to troubleshoot other parts of my app.

Here's a basic example:

JSONObject jo = loadJSONObject("http://open.mapquestapi.com/nominatim/v1/search.php?format=json&q=Almería,+España");

I've tried catching it with an IOException (which is what it throws) but it says that it is never thrown (even though that is what it is throwing. I've also tried just an Exception, but it still prints out the long stacktrace.

Any idea on how to effectively handle this?

thanks! ak


  • woah there forum autoformatter, that link should just be the href part and have the ',+Espana'.


  • In general, Processing doesn't let exceptions to go to the sketch. It catches them itself, then display the trace in the console, which is what you see. And that's why you cannot catch them yourself.

  • edited December 2014

    @PhiLho :

    What is the solution then?

    maybe assigning null first?

    so could he say

    JSONObject jo = null;
    jo = loadJSONObject(............);
    if (jo!=null) 
        println("JSON failed");

    or using try .... catch..... ?

  • Answer ✓

    As said, you cannot use try / catch, since there is no exception to catch.

    If the purpose is to avoid spamming the console, you have to copy the code from Processing, and handle the exception yourself.

  • Hmm, Thanks for the ideas guys.

    I ended up finding using a URI fixed the console spam:

          URI uri= new URI(
          JSONArray j = loadJSONArray(uri.toASCIIString());

    It would be nice to have a 'verbose' flag or something to disable unnecessary logs.

    Thanks! ak

Sign In or Register to comment.