Java异常处理之InvocationTargetException(反射异常)
Java异常处理之InvocationTargetException(反射异常)
InvocationTargetException异常由Method.invoke(obj, )⽅法抛出。当被调⽤的⽅法的内部抛出了异常⽽没有被捕获时,将由此异常接收
⽰例:
1. flect;
2.
3. public class Reflect {
4. public void run(int i) throws ZeroException {
5. B b = new B();
6. b.run(i);
7. }
8. }
9.
10. class B {
11. public void run(int i) throws ZeroException {
12. if (i < 0) {
13. throw new ZeroException("参数不能⼩于零!");
14. }
15. System.out.println("参数:" + i);
16.
17. }
18. }
19.职场心态
20. class ZeroException extends Exception {
21. private static final long rialVersionUID = 1L;
22.
23. private String detailMessage;
24.
25. public ZeroException(String detailMessage) {
26. this.detailMessage = detailMessage;
27. }
28.
29. public String getMessage() {
30. return detailMessage;
31. }
32. }
测试:
1. flect;
2.
3. import flect.InvocationTargetException;
4. import flect.Method;
5.
6. public class Test {
7. public static void main(String[] args) {
8. try {
9. Class<?> clazz = Class.forName("st.reflect.Reflect");
10. Method method = Method("run", int.class);
11. method.wInstance(), -1);
12. } catch (ClassNotFoundException e) {
什么地摸13. e.printStackTrace();
14. } catch (SecurityException e) {
15. e.printStackTrace();
16. } catch (NoSuchMethodException e) {
17. e.printStackTrace();
18. } catch (IllegalArgumentException e) {
19. e.printStackTrace();
20. } catch (IllegalAccessException e) {
21. e.printStackTrace();
22. } catch (InvocationTargetException e) {
什么牌子音响好23. System.out.println("此处接收被调⽤⽅法内部未被捕获的异常");
24. e.printStackTrace();
火锅的食材有哪些25. } catch (InstantiationException e) {
26. e.printStackTrace();
27. }
28. }
29. }
输出:
1. 此处接收被调⽤⽅法内部未被捕获的异常
2. flect.InvocationTargetException
3. flect.NativeMethodAccessorImpl.invoke0(Native Method)
4. flect.NativeMethodAccessorImpl.invoke(Unknown Source)
5. flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
6. at flect.Method.invoke(Unknown Source)
7. flect.Test.main(Test.java:11)
8. Caud by: st.reflect.ZeroException: 参数不能⼩于零!
9. flect.B.run(Reflect.java:13)
10. flect.Reflect.run(Reflect.java:6)
11. ... 5 more
也可以直接打印⽬标异常:
1. flect;
2.
3. import flect.InvocationTargetException;
4. import flect.Method;
5.
档案室工作总结
6. public class Test {
7. public static void main(String[] args) {
邹忌修八尺有余8. try {
9. Class<?> clazz = Class.forName("st.reflect.Reflect");
10. Method method = Method("run", int.class);
11. method.wInstance(), -1);
12. } catch (ClassNotFoundException e) {
13. e.printStackTrace();
14. } catch (SecurityException e) {
15. e.printStackTrace();
16. } catch (NoSuchMethodException e) {
17. e.printStackTrace();
唐诗十首
18. } catch (IllegalArgumentException e) {
19. e.printStackTrace();
20. } catch (IllegalAccessException e) {
21. e.printStackTrace();
22. } catch (InvocationTargetException e) {
23. System.out.println("此处接收被调⽤⽅法内部未被捕获的异常");
24. Throwable t = e.getTargetException();// 获取⽬标异常
25. t.printStackTrace();
26. } catch (InstantiationException e) {
27. e.printStackTrace();
28. }
29. }
30. }
输出:
萨瓦迪卡歌曲
1. 此处接收被调⽤⽅法内部未被捕获的异常
2. st.reflect.ZeroException: 参数不能⼩于零!
3. flect.B.run(Reflect.java:13)
4. flect.Reflect.run(Reflect.java:6)
5. flect.NativeMethodAccessorImpl.invoke0(Native Method)
6. flect.NativeMethodAccessorImpl.invoke(Unknown Source)
7. flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
8. at flect.Method.invoke(Unknown Source)
9. flect.Test.main(Test.java:11)
10.