即使拋出異常,爲什麼下面的代碼總是返回true?爲什麼Java在catch塊內返回statment不工作?
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 2010-02-17 11:13:00
它返回false時,當我有一個錯誤,在調用此代碼時必須有另一個問題 – 2010-02-17 11:13:54
@Bozho:以及我在終端中看到異常警告。 – 2010-02-17 11:39:30