Which of the following code is better to implement
public string myfunc(JSONArray funcargs){
int ctxHandle;
String StringParam=null;
JSONObject jObj=null;
try {
ctxHandle = funcargs.getInt(2);
if(funcargs.length() == 4) {
try {
StringParam = funcargs.getString(3);
} catch (JSONException jsonEx) {
jObj = funcargs.getJSONObject(3);
}
}
} catch (JSONException jsonEx) {
SendJS = "javascript:" + FailureCallBack + "('" + jsonEx.getMessage() + "')";
sendJavascript(SendJS);
return null;
}
//This will return an integer
ret_val = get_data(ctxHandle);
SendJS = "javascript:" + SuccessCallBack + "('" + Integer.toString(ret_val);
if(StringParam != null)
SendJS += "','" + StringParam + "')";
else if(jObj != null)
SendJS += "','" + jObj + "')";
else
SendJS += "')";
sendJavascript(SendJS);
return null;
}
OR
public string myfunc(JSONArray funcargs){
int ctxHandle;
String StringParam=null;
JSONObject jObj=null;
try{
ctxHandle = funcargs.getInt(2);
//This will return an integer
ret_val = get_data(ctxHandle);
SendJS = "javascript:" + SuccessCallBack + "('" + Integer.toString(ret_val);
if(funcargs.length() == 4) {
try {
StringParam = funcargs.getString(3);
SendJS += "','" + StringParam + "')";
} catch (JSONException jsonEx) {
jObj = funcargs.getJSONObject(3);
SendJS += "','" + jObj + "')";
}
SendJS += "')";
}
} catch (JSONException jsonEx) {
SendJS = "javascript:" + FailureCallBack + "('" + jsonEx.getMessage() + "')";
}
sendJavascript(SendJS);
return null;
}
Personally from Java if vs. try/catch overhead Thread I understand that try/catch
should be used only in the cases where you are uncertain of the result and an exception can be handled but the if else if can be saved argument generates some doubt although putting the entire code in a try catch block somehow does not make sense to me.
Need help on this.