2012-11-28 95 views
3

我在Android開發中總是noob。我仍在學習,而且我正在開發我的應用開發的第一步。Android:未處理的異常類型IOException錯誤

我有這個代碼工作,它運行良好或正常的Java,現在我試圖實現Android操作系統。

在我的代碼裏,它表示TEST.openStream()我得到了Unhandled exception type IOException錯誤。

package com.zv.android; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.app.Activity; 
import android.os.Bundle; 

public class ZipActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     try { 
      URL TEST = new URL("http://www.google.com/"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream())); 

      String inputLine; 
      int line=0; 
      while ((inputLine = in.readLine()) != null){ 
       line++; 
         //System.out.println(line + "\t" + inputLine); 
      } 
      // in.close(); 
     } catch(MalformedURLException e) { 
      //Do something with the exception. 
     } 

    } 
} 

回答

9

錯誤的信息很簡單:你需要趕上IOException,這是因爲URL.openStream()被聲明爲

public final InputStream openStream() throws IOException 

通過接受該法的合同也接受這樣的事實,你必須處理這個異常,這它是如何在Java中工作的。這是一個檢查異常,那麼它必須被捕獲,因爲這種異常表示可能出現的情況,並且您的代碼必須處理。

要趕上它只是增加你的try聲明另一種情況:

try { 
    .. 
catch (MalformedURLException e) { 
    .. 
} 
catch (IOException e) { 
    .. 
} 

正如最後請注意:你不需要抓住它,當你調用openStream()方法,你可以指出該方法呼叫openStream()會將異常轉發給調用者,但在調用鏈的末尾,無論如何你都必須捕捉它。

3

IOException也一樣,你是怎麼做到的MalformedURLException(或)申報方法拋出IOException

try { 
      URL TEST = new URL("http://www.google.com/"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream())); 

      String inputLine; 
      int line=0; 
      while ((inputLine = in.readLine()) != null){ 
       line++; 
         //System.out.println(line + "\t" + inputLine); 
      } 
      // in.close(); 
     } catch(MalformedURLException e) { 
      //Do something with the exception. 
     } catch(IOException e2) { 
      //Do something with the exception. 
     } 

IOException是檢查異常,它需要被catch(或)重新拋出。有關更多信息,請參見tutorial

您需要趕上IOException,因爲openStream()方法可能會在異常情況下拋出IOException。

+0

你能解釋一下爲什麼我們也會發生IOException嗎? – Mowgli

+0

@Mowgli:查看更新。 – kosa

1
TEST.openStream() 

可能拋出IOException這是一個在Java檢查的異常,所以你必須要麼手柄使用的try/catch IOException異常塊或使用拋出你的方法簽名聲明 IOException異常條款。

你必須在catch塊中處理IOException。

try { 
      URL TEST = new URL("http://www.google.com/"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream())); 
      //rest of your code 
      } catch(MalformedURLException e) { 
      //Do something with the exception. 
     } 

     catch(MalformedURLException e) { 
     //Do something with the exception. 
     } 
     catch(IOException ex) { 
      ex.printStackTrace(); 
     } 

在你的方法簽名聲明IOException異常:

public void onCreate(Bundle savedInstanceState) throws IOException { 
在這種情況下,你不換一個try/catch內部的代碼

,雖然我會強烈建議總是使用try/catch語句處理異常,而不是用throws子句聲明它。

相關問題