java中如何一次抛出多个异常

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:53:40

  基本思路就是定义三个类,继承异常的父类,然后在需要抛出异常的地方,throws一下就可以了,示例如下:

public class CatchMultiException {
 
    public static void main(String[] args) throws Exception {
        try {
            test(2);
        } catch (Exception e) {
            if (e instanceof TestAException || e instanceof TestBException
                    || e instanceof TestCException) {
                e.printStackTrace