以下哪个代码更好地实现
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;
}
或者
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;
}
个人来自Java if vs. try/catch 开销线程我知道try/catch
应该只在你不确定结果并且可以处理异常但 if else if can be saved 参数产生一些疑问的情况下,尽管把整个try catch 块中的代码对我来说毫无意义。
在这方面需要帮助。