-5

如果我删除“throws 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'); 
    }
}   
4

1 回答 1

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();
   }
}
于 2013-10-22T14:23:00.193 回答