【发布时间】:2016-06-09 10:32:15
【问题描述】:
我正在学习如何使用此代码在 java 中创建文件和目录。
在ERROR LINE 上,我收到错误消息,因为“此块中永远不会抛出 IOException”。
那么我怎么知道哪个函数抛出了什么类型的异常呢?
或者,如果我不确定是否应该在每个 catch 块中使用通用 Exception。
public class FileTest {
public static void main(String[] args) {
//file creation
boolean flag = false;
File file = new File("/IdeaProjects/JavaCode/jstest.txt");
try {
flag = file.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
System.out.println("file path is : " + file.getPath());
//dir creation
boolean dirFlag = false;
File fileDir = new File("/IdeaProjects/JavaCode/js");
try{
dirFlag = fileDir.mkdir();
}catch (IOException e){//ERROR LINE
e.printStackTrace();
}
if(dirFlag)
System.out.println("created");
else
System.out.println("exist");
}
}
【问题讨论】:
-
您的 try 块中只有一个方法调用。阅读该方法的 Javadoc,看看它可能会抛出什么。