簡體   English   中英

在一個 catch 塊中處理多種不同類型的異常?

[英]Multiple different types of exception handling in one catch block?

如果在 catch() 中允許多個異常,那么它將減少冗余錯誤處理代碼的數量。 例如,

try{
// some statments 
}
catch(Type1Exception t1, Type2Exception t2, Type3Exception t3) {   // wish if this could be allowed
/* t1, t2, t3 are children of Exception and needs same error handling then why to have different catch blocks with same piece of code */
}

是的 - 這就是Java 7 支持它的原因。

所以你的例子實際上是:

try {
} catch (Type1Exception | Type2Exception | Type3Exception ex) {
   ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM