为什么它打印出
IOException
无法访问,例如,如果我可以写错误的文件路径,并且它必须捕获它。
public static void main(String[] args) {
try {
File first_name = new File("SomePath\\Next_Text.txt");
File second_name = new File("SomePath\\Renamed_Text.txt");
boolean IsRenamed = first_name.renameTo(second_name);
System.out.println("File succesfully renamed");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.getStackTrace();
}
}
我预计我必须先写2个catch才能catch
IOException
,然后renameTo()
try 块中没有方法会抛出
IOException
。
由于
IOException
是受检查异常,因此必须在方法的签名中声明它们,例如 public void method() throws IOException
顺便说一句:注意 java 命名约定。所有名称都应该是驼峰命名法而不是蛇形命名法,并且变量名称应该以小写字符开头