3
A
回答
0
這取決於具體情況。
1)對於要處理錯誤(如「FileOutputStream中」個案會拋出「FileNotFoundException異常」
try{FileOutputStream out = new FileOutputStream(
new File(System.getProperty("user.dir") + "\\needful\\output.xlsx"));
workbook.write(out);
workbook.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
如果你希望你的代碼運行,即使當某些功能失敗2)。 (如審覈認定)
Assert.assertEquals(21, 25); //here the code execution stops
//here the code fails yet the script will continue its execution.
try{
Assert.assertEquals(21, 25);
}catch(Exception e){
e.printStackTrace();
}
3)如果你想加入「終於{}」功能,使「最後條款中的代碼就一定會執行,即便異常是從內引發嘗試或捕捉塊「。 - Link
public void open_File(){
FileReader reader = null;
try {
reader = new FileReader("file\path\");
//do something here
} catch (IOException e) {
//do something clever with the exception
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
//do something clever with the exception
}
}
System.out.println("--- File Process Ended ---");
}
}
相關問題
- 1. 如何在try catch塊
- 2. TYPO3 6.2如何使用try-catch塊
- 3. 如何使用try-catch塊進行PDO
- 4. C# - 如何try catch塊
- 5. 在try catch塊
- 6. 如何使@try @catch
- 7. 如何使用Try Catch
- 8. 如何使用try catch java?
- 9. 如何使用Assert和Try Catch在硒c#
- 10. 在Java中的try-catch塊中嵌入try-catch塊
- 11. 如何在NSIS中使用try catch
- 12. 如何在JavaFX中使用try-catch?
- 13. 使用InputMismatchException try/catch塊
- 14. Powershell:如何從try-catch塊中退出?
- 15. 是否可以在catch塊中使用Try catch塊?
- 16. 如何在try catch塊中使用for循環
- 17. 如何在異常處理中使用try-catch塊
- 18. 如何在Java中正確使用try-catch-finally塊?
- 19. 如何在vb.net中使用try和catch塊?
- 20. 如何在Page.RegisterAsyncTask方法中使用try catch塊
- 21. try catch塊
- 22. try catch塊
- 23. try-catch塊
- 24. try catch塊宏
- 25. 的try/catch塊
- 26. 如何使用try catch在x =否定時拋出異常
- 27. 自定義等待try catch webdriver
- 28. 使用沒有'catch'塊的'try-finally'塊
- 29. java.lang.OutOfMemoryError即使在try-catch塊中?
- 30. 如何調試try和catch塊?
這取決於你的代碼,當你調用任何方法,只要該方法會拋出異常,你需要抓住他們...... –