【问题标题】:java try catch and returnjava尝试捕获并返回
【发布时间】:2011-10-20 19:22:08
【问题描述】:

我在 java 中有一个执行 HTTP POST 并返回 JSON 对象的小函数。此函数返回 JSON 对象。

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

这向我显示了函数必须返回 JSONObject 的错误。我如何使它工作?出现错误时我无法发送 JSONObject,可以吗?发送一个空白的jsonobject是没有用的

【问题讨论】:

    标签: java json httpclient


    【解决方案1】:

    这是因为如果一切顺利,您只会返回 JSONObject。但是,如果抛出异常,您将进入 catch 块并且不会从函数返回任何内容。

    你需要

    • 在 catch 块中返回一些东西。例如:

      //...
      catch(Exception e) {
          return null;
      }
      //...
      
    • 在 catch 块之后返回一些东西。例如:

      //...
      catch (Exception e) {
          //You should probably at least log a message here but we'll ignore that for brevity.
      }
      return null;
      
    • 从方法中抛出异常(如果选择此选项,则需要将throws 添加到send_data 的声明中)。

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

    【讨论】:

    • 捕获块。 catch 不是循环!
    • @Shaded,DwB 哎呀。感谢您了解这一点。
    【解决方案2】:

    你可以改成这样:

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

    【讨论】:

      【解决方案3】:

      函数中有一条不返回任何内容的路径;编译器不喜欢这样。

      你可以改成

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

      【讨论】:

        【解决方案4】:

        即使在错误情况下也可以返回“某物”。 查看 JSend 以了解标准化您的回复的方法 - http://labs.omniti.com/labs/jsend

        在我看来,最简单的方法是返回一个错误 json 对象并在客户端进行处理,然后完全依赖 HTTP 错误代码,因为并非所有框架都尽可能地处理这些错误代码。

        【讨论】:

          【解决方案5】:

          send_data() 方法应该抛出异常,以便调用 send_data() 的代码可以控制它要如何处理异常。

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

          【讨论】:

            【解决方案6】:

            我更喜欢一进一出。这样的事情在我看来是合理的:

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

            【讨论】:

              猜你喜欢
              • 2016-05-02
              • 2012-06-14
              • 1970-01-01
              • 1970-01-01
              • 2019-04-14
              • 1970-01-01
              • 2017-09-12
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多