예외가 throw 된 경우에도 다음 코드가 항상 true를 반환하는 이유는 무엇입니까?catch 블록 내부의 Java return statement가 작동하지 않는 이유는 무엇입니까?
public boolean write (ArrayList<String> inputText, String locationToSave){
try {
File fileDir = new File(locationToSave);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir), "utf8"));
int index = 0;
int size = inputText.size();
while (index < size) {
out.append(inputText.get(index));
out.append("\n");
index++;
}
out.flush();
out.close();
return true;
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException is : \n" + e.getMessage());
return false;
} catch (IOException e) {
System.out.println("IOException is : \n" + e.getMessage());
return false;
} catch (Exception e) {
System.out.println("Exception is : \n" + e.getMessage());
return false;
}
}
판 이것은 내가 이전 코드 테스트하기 위해 사용하고 코드 01
:
if (fileReader.write(fileReader.read(selectedFile), selectedSaveLocation)) {
System.out.println("The file : " + selectedFile + " as been successfully"
+ "converted to : " + selectedSaveLocation);
} else {
System.out.println("The file : " + selectedFile + " failed to convert!");
}
방법 그것이 던져 졌는지 확인합니까? – Bozho
오류가 발생하면 여기에 false를 반환합니다.이 코드를 호출하는 동안 다른 문제가있을 수 있습니다. –
@Bozho : 터미널에 예외 경고가 표시됩니다. –