Question

I retrieve data from twitter. I ve stored some ids in a database and I am trying to retrieve informations with twitter API. I am using the following code:

if(cursor.hasNext()){
    try { 
        while (cursor.hasNext()) {

            final DBObject result = cursor.next();
            JSONObject features = new JSONObject();

            //System.out.println(result);
            Map<String, Object> value = (Map<String, Object>) result.get("user");
            boole.add((Boolean) value.get("default_profile")); 
            boole.add((Boolean) value.get("default_profile_image"));

            features.put("_id", value.get("id"));
     ...
     }
 catch (JSONException e) {
            System.err.println("JSONException while retrieving users from db: " + e);
        } catch (TwitterException e) {
            // do not throw if user has protected tweets, or if they deleted their account
            if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {


            } else {
                throw e;
            }
        }

I add twitter Exception since I cant retrieve data from some users due to authentication issues. However when my code reach catch (TwitterException e) it automatically stop running. I want to continue to the next cursor (next database's id)

Was it helpful?

Solution

It is because you have try catch block around your loop, so when exception is caught it falls out of the loop.

Placing try catch block inside while block will solve the problem.

OTHER TIPS

You need to move the try-catch block inside the while if you want to do that. Currently, once an exception is thrown in the while, it exits the while and goes to the catch block and the execution stops post that. If you move the try inside the while, the while will continue even after a exception is thrown and handled.

The loop and the try-catch should be structured like this.

while (cursor.hasNext()) {
    try {
        // The code
    }
    catch (JSONException e) {
        // your code
    }
    catch (TwitterException e) {
        // your code
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top