Program crashes when internet connection drops

edited December 2015 in Programming Questions

I am building a weather-station app by requesting JSON data from a weather website. When i have internet connection everything works fine but sometimes my internet drops and processing throws a 'ConnectException' which is to be expected because there is no internet available, i am trying to solve this issue by using try-catch as explained here : but it is not working because it says that 'ConnectException e' is an "Unreachable catch block for ConnectException.This exception is never thrown from the try statement body" but i have imported 'import;' but it still does not work. Can anybody explain what i am doing wrong or what i should do to prevent the program from crashing when i loose internet connection?


  void update() {


    json = loadJSONObject(",PT&units=metric&appid=865339b01d84d874f8dc4857e5eb5f5c");
    if(json != null){
      JSONObject coord = json.getJSONObject("coord");
    lon = coord.getFloat("lon");
    lat = coord.getFloat("lat");

    JSONObject sys = json.getJSONObject("sys");
    sunR = sys.getInt("sunrise");
    sunS = sys.getInt("sunset");

    JSONObject main = json.getJSONObject("main");
    temp = main.getFloat("temp");
    pressure = main.getFloat("pressure");
    humidity = main.getInt("humidity");

    JSONObject wind = json.getJSONObject("wind");
    windS = wind.getFloat("speed");
    windD = wind.getFloat("deg");

    JSONObject clouds = json.getJSONObject("clouds");
    cloud = clouds.getInt("all");

    JSONArray weather = json.getJSONArray("weather");
    JSONObject mainCond = weather.getJSONObject(0);
    ID = mainCond.getInt("id");
    condition = mainCond.getString("main");
    description = mainCond.getString("description");
    icon = mainCond.getString("icon");

    ImageAdress = "" + icon + ".png";
    weatherIcon = loadImage(ImageAdress);

      println(" INTERNET");
}catch(ConnectException e){
  println("no Internet");

    weather_counter = millis();


  • edited December 2015

    Most of Processing's functions "swallow" any thrown Exception.
    In the case of loadJSONObject(), it "swallows" IOException.
    However, an additional NullPointerException somehow slips from its buggy grasp.

    Thus it's NullPointerException which should go into catch ().
    Also, no need to wrap up big chunks of code inside try.
    Just focus the portion which really needs to be there: :-B

    // program-crashes-when-internet-connection-drops
    // GoToLoop (2015-Dec-16)
    static final String URL = "http:/" + "/"
      + "/data/2.5/" + "weather" + "?q=Alcafache,PT&units=metric"
      + "&appid=865339b01d84d874f8dc4857e5eb5f5c";
    void setup() {
      JSONObject json = loadJSON(URL);
      json = loadJSON(URL + "GonnaFailThisTime!");
    JSONObject loadJSON(String path) {
      try {
        return loadJSONObject(path);
      catch (NullPointerException e) {
        return null;
  • edited December 2015

    Thanks for the reply, i will integrate it into my program and give it a try, i assume that i can remove the code below, is this correct?

          json = loadJSON(URL + "GonnaFailThisTime!");
  • edited December 2015

    Yea, those extra lines were there just to force the error in loadJSONObject() to trigger. >-)
    And make sure my custom loadJSON() function would successfully recover from it. :-\"

Sign In or Register to comment.