2011-10-20 4 views
2

Java에서 HTTP POST를 수행하는 JSON 객체를 반환하는 작은 함수가 있습니다. 이 함수는 JSON 객체를 반환합니다.java try catch 및 return

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를 보내면 쓸모가 없습니다.

답변

0

아무 것도 반환하지 않는 함수가있는 경로가 있습니다. 컴파일러는 그것을 좋아하지 않습니다.

당신은 모든 것이 원활하게 진행하는 경우에만 JSONObject을 반환되기 때문이다

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

로 변경할 수 있습니다. 그러나 예외가 발생하면 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; 
    
  • 는 (이 옵션을 선택하면, 당신은 send_data의 선언에 throws를 추가해야합니다) 메서드에서 예외를 던져.

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

그냥 의미가 있지만 ... -lop-catch? – Shaded

+0

catch 블록 캐치는 루프가 아닙니다! – DwB

+1

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

심지어 오류 상태에서 '무언가'를 반환하는 reasonble입니다. 방법에 대한 JSend에서 봐 당신의 응답을 표준화하기 - 제 생각에는 http://labs.omniti.com/labs/jsend

는 모든 프레임 워크는 그 처리 이후 다음 단독으로 HTTP 오류 코드에 의존하는 클라이언트 측에서 그 오류 JSON 개체를 반환하고 처리하는 가장 쉬운 방법 그들이 할 수있을뿐만 아니라.

0

한 항목과 한 항목을 선호합니다. 이런 식으로 뭔가 나에게 합리적인 보인다 send_data()를 호출하는 코드가 예외를 처리하고자하는 방법을 제어 할 수 있도록

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

send_data() 메소드가 예외를 throw합니다.

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