2016-11-11 40 views

回答

4

是的,可以使用Java 7+中的多個catch塊。

您可以這樣做來實現在遇到特定錯誤時執行的特定方案。例如: 來源:Jenkov.com

try { 

    // execute code that may throw 1 of the 3 exceptions below. 

} catch(SQLException | IOException e) { 
    logger.log(e); 

} catch(Exception e) { 
    logger.severe(e); 
} 
+0

我寧願看到SQLException | IOException如果catch體是相同的。你在教新手不良習慣。 – Bathsheba

+0

@Bathsheba我沒有寫這個例子,正如我的答案中所述。 –

+0

這並不能改變這個例子是一個糟糕的例子。 – Bathsheba

相關問題