异常处理

来源:互联网 发布:山西网络广播电视台 编辑:程序博客网 时间:2024/05/22 18:53

1.catch块之所以会抛出异常,是因为try块调用的方法本身被声明为会throws Exception,比如:

File f = new File("XXX");FileInputStream inStream = new FileInputStream(f);

第二行代码之所以要加异常处理,是一位FileInputStream类中的FileInputStream(File file)方法会throws FileNotFoundException ,所以当文件不存在的时候,该方法就会抛出异常。

File f = new File("XXX");try{    FileInputStream inStream = new FileInputStream(f);}catch(Exception e){System.out.println(e)}