[JAVA]运用反射机制,抛出的异常的捕获方法

背景介绍

笔者前些日子做过一个这样的事情,利用反射机制调用一个会报Checked的异常的方法,在调用后没法抛出异常(原因是反射机制调用是没法catch到Checked异常的)。举个简单的例子如下。 
1.先定义一个简单的异常类(这样的定义是Checked的异常哦)

package com.zju.study.exception;

public class SimpleException extends Exception {
    private static final long serialVersionUID = 1L;
    private String Message;

    public SimpleException(String message) {
        this.setMessage(message);
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2.定义一个实体类Man

package com.zju.study.exception;

public class Man {
    private String work;

    public String getWork() {
        return work;
    }

    public void setWork(String work) throws SimpleException {
        this.work = work;
        System.out.println(work);
        throw new SimpleException("man is died");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.最后定义Main方法。

package com.zju.study.exception;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) {
        Method method = null;
        try {
            method = Man.class.getMethod("setWork", String.class);
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
        Man man = new Man();
        try {
            method.invoke(man, "man is hard working");
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

OK~~运行之,结果如下。

man is hard working
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.zju.study.exception.Main.main(Main.java:16)
Caused by: com.zju.study.exception.SimpleException: man is died
    at com.zju.study.exception.Man.setWork(Man.java:13)
    ... 5 more
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这个时候的需求是!!!在invoke方法调用抛出的异常在Main中被捕获,那该如何去做呢?

大多数人可能和我一样,觉得这种问题很简单,加个catch语句呗。。如下。

catch (SimpleException e) {
            e.printStackTrace();
        }
  • 1
  • 2
  • 3

结果,报错了!!!如下。

Unreachable catch block for SimpleException. This exception is never thrown from the try statement body
  • 1

这里我们给出解决办法如下。 
一个重要的方法,getCause()方法。具体代码如下。

package com.zju.study.exception;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) {
        Method method = null;
        try {
            method = Man.class.getMethod("setWork", String.class);
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
        Man man = new Man();
        try {
            method.invoke(man, "man is hard working");
        } catch (SimpleException e) {
            e.printStackTrace();
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            Throwable cause = e.getCause();
            if(cause instanceof SimpleException){
                System.out.println("ok,we catch SimpleException:"+(SimpleException)cause);
            }

        }

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

运行结果如下。

man is hard working
ok,we catch SimpleException:com.zju.study.exception.SimpleException: man is died

猜你喜欢

转载自blog.csdn.net/happyzwh/article/details/79636924