multi-catch和try-catch异常处理知识点详解
multi-catch属于JDK1.7之后出现的,举例如下:
class FactoryTest {
public static Fruits getInstance(String className){
Fruits fruits=null;
try {
fruits=(Fruits)Class.forName(className).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fruits;
}
}
===============================================
class FactoryTest {
public static Fruits getInstance(String className){
Fruits fruits=null;
try {
fruits=(Fruits)Class.forName(className).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fruits;
}
}
如果使用try-catch,会有多个catch块,从里到外是之类异常和父类异常,但是使用了multi-catch之后只需要有一个catch块就可以了,括号里面有三个异常,需要注意的是这三个异常不能有交集,作为科班出生的我不想解释什么是交集!
以上是 multi-catch和try-catch异常处理知识点详解 的全部内容, 来源链接: utcz.com/z/341866.html