-2

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.

Community
  • 1
  • 1
Nathan
  • 674
  • 8
  • 22
  • 4
    can you remove the non-relevant pieces from the code? And point out the part that differs? – Thilo Jul 05 '12 at 06:57
  • @Thilo The whole idea is to analyse the function as a whole rather than just see it in bits and pieces and its just a 15 lines of code. So I posted :) – Nathan Jul 05 '12 at 07:15

2 Answers2

1

The code is doesn't do the same thing, but if you fixed the second example, I would say its better because you are placing related code together.

However, I would suggest you should do what you believe is clearer and since the first example is probably right, it may be a better choice for you.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Usually we use try catch in the places where we expect some errors. for example you are going to divide a number with an another number, and it is zero , surely it will throw error. there we will use try catch.

That is for catching run time errors, we use try catch.

It is not a must that you should use try catch in all you codes, but using try catch will reduce the chances for showing exception.

Sarin Jacob Sunny
  • 2,138
  • 3
  • 29
  • 61