简体   繁体   中英

java try catch and return

I have a small function in java that does a HTTP POST, and returns a JSON Object. This function return the JSON Object.

public JSONObject send_data(ArrayList<NameValuePair> params){
    JSONObject response;
    try {
        response = new JSONObject(CustomHttpClient.executeHttpPost(URL, params).toString());
        return response;
    } catch(Exception e) {
        // do smthng
    }
}

This shows me an error that the function must return a JSONObject. how do i make it work? I cant send a JSONObject when there is an error, can I? It would be useless to send a blank jsonobject

This is because you are only returning a JSONObject if everything goes smoothly. However, if an exception gets thrown, you will enter the catch block and not return anything from the function.

You need to either

  • Return something in the catch block. For example:

     //... catch(Exception e) { return null; } //... 
  • Return something after the catch block. For example:

     //... catch (Exception e) { //You should probably at least log a message here but we'll ignore that for brevity. } return null; 
  • Throw an exception out of the method (if you choose this option, you will need to add throws to the declaration of send_data ).

     public JSONObject send_data(ArrayList<NameValuePair> params) throws Exception { return new JSONObject(CustomHttpClient.executeHttpPost(URL, params).toString()); } 

You could change it to this:

public JSONObject send_data(ArrayList<NameValuePair> params){
    JSONObject response = null;
    try {
        response = new JSONObject(CustomHttpClient.executeHttpPost(URL, params).toString());
    } catch(Exception e) {
        // do smthng
    }

    return response;
}

There's a path through the function that doesn't return anything; the compiler doesn't like that.

You can change this to

catch(Exception e) {
    // do smthng
    return null; <-- added line
}
or put the return null (or some reasonable default value) after the exception block.

It's reasonble to return 'something' even in an error condition. Look at JSend for a way to standardize your responses - http://labs.omniti.com/labs/jsend

In my opinion it's easiest to return an error json object and handle that on the client side then to solely rely on HTTP error codes since not all frameworks deal with those as well as they could.

I prefer one entry and one exit. Something like this seems reasonable to me:

public JSONObject send_data(ArrayList<NameValuePair> params)
{
    JSONObject returnValue;
    try
    {
        returnValue = new JSONObject(CustomHttpClient.executeHttpPost(URL, params).toString());
    }
    catch (Exception e)
    {
        returnValue = new JSONObject(); // empty json object .
        // returnValue = null; // null if you like.
    }

    return returnValue;
}

The send_data() method should throw an exception so that the code calling send_data() has control over how it wants to handle the exception.

public JSONObject send_data(ArrayList<NameValuePair> params) throws Exception {
  JSONObject response = new JSONObject(CustomHttpClient.executeHttpPost(URL, params).toString());
  return response;
}

public void someOtherMethod(){
  try{
    JSONObject response = sendData(...);
    //...
  } catch (Exception e){
    //do something like print an error message
    System.out.println("Error sending request: " + e.getMessage());
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM