throws ? catch checked unchecked

ThrowableClass

  Error  (unchecked)     

  Exception

    IOException (checked)

    RuntimeException (unchecked)  

 

 

public void read(String filename)
{
    try
    {
        InputStream in = new FileInputStream(filename);
        int b;
        while((b = in.read()) != -1){
            process input
        }
    }
    catch(IOException exception)
    {
        exception.printStackTrace();
    }
}



//较好
//read方法出现了错误,就让read方法的调用者去处理
public void read(String filename) throws IOException
{
    InputStream in = new FileInputStream(filename);
    int b;
    while((b = in.read()) != -1){
        process input
    }
}

 

posted @ 2017-10-30 14:58  papering  阅读(171)  评论(0编辑  收藏  举报