Android:未处理的异常类型IOException错误

问题描述:

我在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. 
     } 

    } 
} 

错误的信息很简单:你需要赶上IOException,这是因为URL.openStream()被声明为

public final InputStream openStream() throws IOException 

通过接受该法的合同也接受这样的事实,你必须处理这个异常,这它是如何在Java中工作的。这是一个检查异常,那么它必须被捕获,因为这种异常表示可能出现的情况,并且您的代码必须处理。

要赶上它只是增加你的try声明另一种情况:

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

正如最后请注意:你不需要抓住它,当你调用openStream()方法,你可以指出该方法呼叫openStream()会将异常转发给调用者,但在调用链的末尾,无论如何你都必须捕捉它。

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

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子句声明它。