Amit
Amit

Reputation: 3990

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

Upvotes: 2

Views: 5764

Answers (6)

DwB
DwB

Reputation: 38320

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;
}

Upvotes: 0

Michael
Michael

Reputation: 35341

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());
  }
}

Upvotes: 0

Jack Edmonds
Jack Edmonds

Reputation: 33181

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());
    }
    

Upvotes: 10

Trever Shick
Trever Shick

Reputation: 1784

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.

Upvotes: 0

mamboking
mamboking

Reputation: 4637

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;
}

Upvotes: 2

Steve B.
Steve B.

Reputation: 57325

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.

Upvotes: 0

Related Questions