类别:异常处理
描述:finally代码块中抛出异常
解决办法:合并try,优快云
原始代码:
public static void throwException(){
File file = null;
FileInputStream fis = null;
try{
file = new File("abc.txt");
//可能抛出FileNotFoundException
fis = new FileInputStream(file);
fis.read();
}catch(FileNotFoundException e){
System.out.println("file not found");
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
} finally{
//这里存在一个的异常,需要进行处理
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
修改后代码:
public static void throwException(){
File file = null;
FileInputStream fis = null;
try{
try {
file = new File("abc.txt");
//可能抛出FileNotFoundException
fis = new FileInputStream(file);
fis.read();
} finally {
if(fis != null){
fis.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}