标签:
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块就可以了,括号里面有三个异常,需要注意的是这三个异常不能有交集,作为科班出生的我不想解释什么是交集!
标签:
原文地址:http://www.cnblogs.com/Mindreader/p/5487701.html