zl程序教程

您现在的位置是:首页 >  后端

当前栏目

InvocationTargetException异常解析详解编程语言

异常编程语言 详解 解析
2023-06-13 09:20:38 时间

InvocationTargetException异常由Method.invoke(obj, args )方法抛出。当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收。

示例:


[java] 
view plain
 copy

 print
?

package com.zzj.test.reflect;    public class Reflect {   public void run(int i) throws ZeroException {   B b = new B();   b.run(i);   }  }    class B {   public void run(int i) throws ZeroException {   if (i   0) {   throw new ZeroException( 参数不能小于零! );   }   System.out.println( 参数:  + i);     }  }    class ZeroException extends Exception {   private static final long serialVersionUID = 1L;     private String detailMessage;     public ZeroException(String detailMessage) {   this.detailMessage = detailMessage;   }     public String getMessage() {   return detailMessage;   }  } 
[java] 
view plain
 copy

 print
?
在CODE上查看代码片
派生到我的代码片

package com.zzj.test.reflect;    import java.lang.reflect.InvocationTargetException;  import java.lang.reflect.Method;    public class Test {   public static void main(String[] args) {   try {   Class ?  clazz = Class.forName( com.zzj.test.reflect.Reflect );   Method method = clazz.getMethod( run , int.class);   method.invoke(clazz.newInstance(),  1);   } catch (ClassNotFoundException e) {   e.printStackTrace();   } catch (SecurityException e) {   e.printStackTrace();   } catch (NoSuchMethodException e) {   e.printStackTrace();   } catch (IllegalArgumentException e) {   e.printStackTrace();   } catch (IllegalAccessException e) {   e.printStackTrace();   } catch (InvocationTargetException e) {   System.out.println( 此处接收被调用方法内部未被捕获的异常 );   e.printStackTrace();   } catch (InstantiationException e) {   e.printStackTrace();   }   }  } 
[plain] 
view plain
 copy

 print
?
在CODE上查看代码片
派生到我的代码片

此处接收被调用方法内部未被捕获的异常  java.lang.reflect.InvocationTargetException   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at java.lang.reflect.Method.invoke(Unknown Source)   at com.zzj.test.reflect.Test.main(Test.java:11)  Caused by: com.zzj.test.reflect.ZeroException: 参数不能小于零!   at com.zzj.test.reflect.B.run(Reflect.java:13)   at com.zzj.test.reflect.Reflect.run(Reflect.java:6)     5 more 
[java] 
view plain
 copy

 print
?
在CODE上查看代码片
派生到我的代码片

package com.zzj.test.reflect;    import java.lang.reflect.InvocationTargetException;  import java.lang.reflect.Method;    public class Test {   public static void main(String[] args) {   try {   Class ?  clazz = Class.forName( com.zzj.test.reflect.Reflect );   Method method = clazz.getMethod( run , int.class);   method.invoke(clazz.newInstance(),  1);   } catch (ClassNotFoundException e) {   e.printStackTrace();   } catch (SecurityException e) {   e.printStackTrace();   } catch (NoSuchMethodException e) {   e.printStackTrace();   } catch (IllegalArgumentException e) {   e.printStackTrace();   } catch (IllegalAccessException e) {   e.printStackTrace();   } catch (InvocationTargetException e) {   System.out.println( 此处接收被调用方法内部未被捕获的异常 );   Throwable t = e.getTargetException();// 获取目标异常   t.printStackTrace();   } catch (InstantiationException e) {   e.printStackTrace();   }   }  } 
[plain] 
view plain
 copy

 print
?
在CODE上查看代码片
派生到我的代码片

此处接收被调用方法内部未被捕获的异常  com.zzj.test.reflect.ZeroException: 参数不能小于零!   at com.zzj.test.reflect.B.run(Reflect.java:13)   at com.zzj.test.reflect.Reflect.run(Reflect.java:6)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at java.lang.reflect.Method.invoke(Unknown Source)   at com.zzj.test.reflect.Test.main(Test.java:11)  

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13066.html

cjava