throws ? catch checked unchecked

时间:2022-08-19 05:09:50

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
}
}