我继承了一些编译时出错的代码。我得到两个错误之一:其中一个,return
声明丢失,或另一个我有不匹配的throw
/try
/catch
。这个错误似乎是与return
声明的位置有关,我一直在整个上午都在移动。其当前位置收益率为error: missing return statement
。任何被注释掉的回报都会产生一条消息,我必须声明try
或catch
与我的throw
。JAVA:为什么在编译期间返回错误?
考虑:
static private List<File> getFileListingNoSort(File aDirectory) throws FileNotFoundException {
try {
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aDirectory.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs) {
result.add(file); //always add, even if directory
if (! file.isFile()) {
List<File> deeperList = getFileListingNoSort(file);
result.addAll(deeperList);
// return result;
} //close if
// return result;
} // close for
} // close try for getFileListingNoSort
// return result;
catch (Exception exp) {
System.err.println("Error: " + exp.getMessage());
} //close catch getFileListingNoSort
return result;
} //close method
我认为的回报将在尝试后去的,但赶上了。但是,这并不适合我。我不是很确定为什么我要得到这些地区,除了误解节目如何与try
/catch
流动。有人能告诉我最合适的回归地点,并解释原因吗?