2017-01-10 5 views
0

こんにちは私は自分のコードを共有しています 問題はそれがエラーなしで動作していますが、 "例外JsonExceptionは、サーバー内にあります。対応するtryステートメントの異なる環境でJSONExceptionがスローされることはありません

「JSONExceptionをスローする」という宣言を次の2つのメソッドに追加すると問題は解決されますが、実際はそうしたくありません。その理由は何でしょうか?

private JSONArray convertStringToJsonArray(String source)throws JSONException { 
    return new JSONArray(source); 
} 

private JSONObject getElementIndexAt(JSONArray jsonArray, int index)throws JSONException { 
    return jsonArray.getJSONObject(index); 
} 

public class JsonModifier { 

public String getElementIndexAt(String source, int index){ 
    String output; 

    try 
    { 
     JSONArray jsonArray = convertStringToJsonArray(source); 
     JSONObject jsonObject = getElementIndexAt(jsonArray, index); 
     output = convertJsonToString(jsonObject); 
    } 
    catch(JSONException ex) 
    { 
     log.error("Page not found, exception: " + ex.getMessage()); 
     output = "Page not found, exception: " + ex.getMessage(); 
    } 

    return output; 
} 

private JSONArray convertStringToJsonArray(String source) { 
    return new JSONArray(source); 
} 

private JSONObject getElementIndexAt(JSONArray jsonArray, int index) { 
    return jsonArray.getJSONObject(index); 
} 

private String convertJsonToString(JSONObject jsonObject){ 
    return jsonObject.toString(); 
} 

}

答えて

0

としてdocumentation、getJSONObjectとコンストラクタスロー例外で述べています。したがって、try/catchでこれらの関数を囲まない場合は、その例外を上にスローする必要があります。

注:あなた自身の関数でtry/catchを使用しているので、あなたのソリューションが動作する理由がわかります。

希望します。

関連する問題