簡體   English   中英

使用 try 處理異常

[英]Exception handling with try

我正在使用 try 和 catch 來處理異常。但是當我使用它時說“無法解析符號”我在一個函數的類中使用它但是當我在活動中使用它時沒有發生錯誤。

 public ArrayList<Earthquake> extractEarthquakes = new ArrayList<Earthquake>()
{
    ArrayList<Earthquake> earthquakes = new ArrayList<Earthquake>();

    try
    {
        //Capture and stores the root JSON object
        JSONObject rootJSONObject = ( JSONObject ) new JSONObject( SAMPLE_JASON_RESPONSE );

    }
    catch( JSONException e )
    {
        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
    }

};

嘗試和捕獲無法解決。我該怎么辦?

您只能在函數內使用 try-catch-block。 你需要做的是覆蓋函數,你想使用 try-catch-block in. 類似的東西:

public ArrayList<Earthquake> extractEarthquakes = new ArrayList<Earthquake>() {
                //If you want to override e.g. the get-Function
                @Override
                public Earthquake get(int index) {
                    ArrayList<Earthquake> earthquakes = new ArrayList<Earthquake>();
                    try {
                        //Capture and stores the root JSON object
                        JSONObject rootJSONObject = (JSONObject) new JSONObject(SAMPLE_JASON_RESPONSE);
                    } catch (JSONException e) {
                        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
                    }
                    return super.get(index);
                }
            };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM