![](/img/trans.png)
[英]Approach to handle exception in Spring Boot. Throw exception catch and throw the same exception, is it a bad practice?
[英]when the code throw many exception and the handle is same, which choice is better?
當代碼拋出許多異常並且句柄相同時,哪個選擇更好?
try{
}catch(ExceptionOne e1)
{
someHandle()
}
catch(ExceptionTwo e2){
someHandle()
}
catch(ExceptionThree e3)
{
someHandle()
}
要么
try{
}catch(Exception e1)
{
someHandle()
}
有些書籍在裝飾異常時會嘗試抓住它,不要嘗試cacth所有異常。
哪個更好?
第一個更好。 第二個捕獲所有異常,這可能包括一些您不想捕獲的異常。
如果升級到Java 7,則可以使用可以捕獲多種類型異常的catch塊 ,這是一種更清晰的解決方案。
try {
// Something that might throw.
}
catch(ExceptionOne | ExceptionTwo | ExceptionThree e) {
someHandle()
}
如果你不關心異常是什么,你可以拋棄一般的“異常:”
try {
...
} catch (Exception e) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.