2013-10-22 33 views
-4

我需要在代码中做什么更改如果我删除了“throws IOException”行?抛出的替代代码抛出IOException

import java.io.*; 
class Buffered_class{ 
    public static void main(String[] args) 
        throws IOException // remove this line 
    { 
     char c; 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("Enter characters, 'q' to quit"); 
     do{ 
      c= (char)br.read(); 
      System.out.println(" you entered : " + c); 
     }while(c !='q'); 
    } 
} 
+1

捕获异常。 –

+1

尝试一下,然后回来。更多信息:[Lesson:Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/) –

+0

为什么抛出IOException? –

回答

2

您需要捕获异常

import java.io.*;  
class Buffered_class{ 
    public static void main(String[] args) 
    { 
     char c; 
     try{ 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.print("Enter characters, 'q' to quit");   
      do{ 
       c= (char)br.read(); 
       System.out.println(" you entered : " + c); 

      }while(c !='q'); 
     }catch(IOException e){ 
      // do something 
     }finally{ 
      br.close(); 
    } 
} 
+0

这段代码的问题是你没有关闭'BufferedReader'使用的资源。记得**总是**打电话'关闭'。 –

+0

@LuiggiMendoza你是对的! –

相关问题