我正在写代码,想知道使用异常类捕获异常好还是使用多种类型的try-catch好。
try{
// some error-prone code
}catch(Exception e){
}
或者,
try{
// some error-prone code
}catch(NullPointerException n){
}catch(ArrayOutOfBoundException a){
} ..... etc
最佳答案
这取决于您将如何处理异常。如果您对每个异常有特定的操作,那么您可以使用多个 try catch。如果没有,您可以只使用通用异常。另外,如果您知道您的代码可能会抛出哪种异常,那么最好使用该特定异常。
关于java - 使用异常或者使用多个try catch java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759089/