I am parsing a response from server, and in case it contains the fields "chunk_number"(J_ID_CHUNK_NUMBER) and "total_chunk_number"(J_ID_CHUNK_TOTAL), I want to check whether I should request another chunk or not. Not a complicated task. Yet I doubt what would be a better way to implement?
Option 1 - using try catch:
private int getNextChunkNumber(JSONObject jUpdate) {
try {
int current;
int total;
current = jUpdate.getInt(J_ID_CHUNK_NUMBER);
total = jUpdate.getInt(J_ID_CHUNK_TOTAL);
if (current < total) {
return current + 1;
}
}
catch(JSONException e) {
// This is an empty exception because it merges with the default result (-1)
}
return -1;
}
Option 2 - using the has method:
private int getNextChunkNumber(JSONObject jUpdate) {
int current;
if (jUpdate.has(J_ID_CHUNK_NUMBER) &&
jUpdate.has(J_ID_CHUNK_TOTAL)) {
current = jUpdate.getInt(J_ID_CHUNK_NUMBER);
if (current < jUpdate.getInt(J_ID_CHUNK_TOTAL)) {
return current + 1;
}
}
return -1;
}