if-clause에서 예외 처리가 매우 오류가 발생했습니다. 경로를 찾을 수없는 경우 예외가 발생합니다. 예외는 함수를 다시 리턴합니다. 이 함수는 재귀 적입니다. 안전한?Java : IOException에 의한 위험한 자체 반환 - 재귀 함수?
는$ javac SubDirs.java $ java SubDirs Give me an path. . Dir1 Dir2 Give me an path. IT WON'T FIND ME, SO IT RETURNS ITSELF due to Exception caught Give me an path.
Subdirs.java
의 코드는 다음과 같습니다 : 당신이 잘못된 경로를 수천 번을 입력 할 때까지
import java.io.*;
import java.util.*;
public class SubDirs {
private List<File> getSubdirs(File file) throws IOException {
List<File> subdirs = Arrays.asList(file.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
}));
subdirs = new ArrayList<File>(subdirs);
List<File> deepSubdirs = new ArrayList<File>();
for(File subdir : subdirs) {
deepSubdirs.addAll(getSubdirs(subdir));
}
subdirs.addAll(deepSubdirs);
return subdirs;
}
public static void search() {
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("Give me an path.");
while ((s = in.readLine()) != null && s.length() != 0){
SubDirs dirs = new SubDirs();
List<File> subDirs = dirs.getSubdirs(new File(s));
for (File f : subDirs) {
System.out.println(f.getName());
}
System.out.println("Give me an path.");
}
}catch(Exception e){
// Simple but is it safe?
search();
}
}
public static void main(String[] args) throws IOException {
search();
}
}
질문을 수정 해 주실 수 있습니까? 네가 무엇을 요구하는지 이해하는 데 어려움이있다. –
@Sauer : A를 재귀 함수라고합시다. def로 B는 A를 반환하기 때문에 재귀 적입니다. try-catch-loop를 사용하여 내부 재귀 함수로 재귀 함수를 갖는 것이 안전합니까? – hhh
B 자신을 반환합니다. A 자체를 반환합니다. B는 A를 내부 구조로 갖는다. A는 getSubDirs이고 B는 검색입니다. – hhh