<div id="cnblogs_post_body" class="blogpost-body"><p><strong><span style="font-size: 18pt;"> Java异常处理之InvocationTargetException(反射异常)<br></span></strong></p> <p> </p> <p>InvocationTargetException异常由Method.invoke(obj, args...)方法抛出。<strong><span style="font-size: 16px;"><span style="color: #ff0000;">当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收!!!</span></span></strong></p> <p><strong>示例:</strong></p> <div class="dp-highlighter bg_java"><ol class="dp-j" start="1"> <li class="alt"><span class="keyword">package com.zzj.test.reflect; </span></li> <li> </li> <li class="alt"><span class="keyword">public <span class="keyword">class Reflect { </span></span></li> <li> <span class="keyword">public <span class="keyword">void run(<span class="keyword">int i) <span class="keyword">throws ZeroException { </span></span></span></span></li> <li class="alt"> B b = <span class="keyword">new B(); </span></li> <li> b.run(i); </li> <li class="alt"> } </li> <li>} </li> <li class="alt"> </li> <li><span class="keyword">class B { </span></li> <li class="alt"> <span class="keyword">public <span class="keyword">void run(<span class="keyword">int i) <span class="keyword">throws ZeroException { </span></span></span></span></li> <li> <span class="keyword">if (i < <span class="number">0) { </span></span></li> <li class="alt"> <span class="keyword">throw <span class="keyword">new ZeroException(<span class="string">"参数不能小于零!"); </span></span></span></li> <li> } </li> <li class="alt"> System.out.println(<span class="string">"参数:" + i); </span></li> <li> </li> <li class="alt"> } </li> <li>} </li> <li class="alt"> </li> <li><span class="keyword">class ZeroException <span class="keyword">extends Exception { </span></span></li> <li class="alt"> <span class="keyword">private <span class="keyword">static <span class="keyword">final <span class="keyword">long serialVersionUID = 1L; </span></span></span></span></li> <li> </li> <li class="alt"> <span class="keyword">private String detailMessage; </span></li> <li> </li> <li class="alt"> <span class="keyword">public ZeroException(String detailMessage) { </span></li> <li> <span class="keyword">this.detailMessage = detailMessage; </span></li> <li class="alt"> } </li> <li> </li> <li class="alt"> <span class="keyword">public String getMessage() { </span></li> <li> <span class="keyword">return detailMessage; </span></li> <li class="alt"> } </li> <li>} </li>
</ol></div> <p>测试:</p> <div class="dp-highlighter bg_java"> <div class="bar"> </div> <ol class="dp-j" start="1"> <li class="alt"><span class="keyword">package com.zzj.test.reflect; </span></li> <li> </li> <li class="alt"><span class="keyword">import java.lang.reflect.InvocationTargetException; </span></li> <li><span class="keyword">import java.lang.reflect.Method; </span></li> <li class="alt"> </li> <li><span class="keyword">public <span class="keyword">class Test { </span></span></li> <li class="alt"> <span class="keyword">public <span class="keyword">static <span class="keyword">void main(String[] args) { </span></span></span></li> <li> <span class="keyword">try { </span></li> <li class="alt"> Class<?> clazz = Class.forName(<span class="string">"com.zzj.test.reflect.Reflect"); </span></li> <li> Method method = clazz.getMethod(<span class="string">"run", <span class="keyword">int.<span class="keyword">class); </span></span></span></li> <li class="alt"> method.invoke(clazz.newInstance(), -<span class="number">1); </span></li> <li> } <span class="keyword">catch (ClassNotFoundException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (SecurityException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (NoSuchMethodException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (IllegalArgumentException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (IllegalAccessException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (InvocationTargetException e) { </span></li> <li class="alt"> System.out.println(<span class="string">"此处接收被调用方法内部未被捕获的异常"); </span></li> <li> e.printStackTrace(); </li> <li class="alt"> } <span class="keyword">catch (InstantiationException e) { </span></li> <li> e.printStackTrace(); </li> <li class="alt"> } </li> <li> } </li> <li class="alt">} </li>
</ol></div> <p>输出:</p> <div class="dp-highlighter bg_plain"> <div class="bar"> </div> <ol start="1"> <li class="alt">此处接收被调用方法内部未被捕获的异常 </li> <li>java.lang.reflect.InvocationTargetException </li> <li class="alt"> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) </li> <li> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) </li> <li class="alt"> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) </li> <li> at java.lang.reflect.Method.invoke(Unknown Source) </li> <li class="alt"> at com.zzj.test.reflect.Test.main(Test.java:11) </li> <li>Caused by: com.zzj.test.reflect.ZeroException: 参数不能小于零! </li> <li class="alt"> at com.zzj.test.reflect.B.run(Reflect.java:13) </li> <li> at com.zzj.test.reflect.Reflect.run(Reflect.java:6) </li> <li class="alt"> ... 5 more </li>
</ol></div> <p>也可以直接打印目标异常:</p> <div class="dp-highlighter bg_java"> <div class="bar"> </div> <ol class="dp-j" start="1"> <li class="alt"><span class="keyword">package com.zzj.test.reflect; </span></li> <li> </li> <li class="alt"><span class="keyword">import java.lang.reflect.InvocationTargetException; </span></li> <li><span class="keyword">import java.lang.reflect.Method; </span></li> <li class="alt"> </li> <li><span class="keyword">public <span class="keyword">class Test { </span></span></li> <li class="alt"> <span class="keyword">public <span class="keyword">static <span class="keyword">void main(String[] args) { </span></span></span></li> <li> <span class="keyword">try { </span></li> <li class="alt"> Class<?> clazz = Class.forName(<span class="string">"com.zzj.test.reflect.Reflect"); </span></li> <li> Method method = clazz.getMethod(<span class="string">"run", <span class="keyword">int.<span class="keyword">class); </span></span></span></li> <li class="alt"> method.invoke(clazz.newInstance(), -<span class="number">1); </span></li> <li> } <span class="keyword">catch (ClassNotFoundException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (SecurityException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (NoSuchMethodException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (IllegalArgumentException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (IllegalAccessException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } <span class="keyword">catch (InvocationTargetException e) { </span></li> <li class="alt"> System.out.println(<span class="string">"此处接收被调用方法内部未被捕获的异常"); </span></li> <li> Throwable t = e.getTargetException();<span class="comment">// 获取目标异常 </span></li> <li class="alt"> t.printStackTrace(); </li> <li> } <span class="keyword">catch (InstantiationException e) { </span></li> <li class="alt"> e.printStackTrace(); </li> <li> } </li> <li class="alt"> } </li> <li>} </li>
</ol></div> <p>输出:</p> <div class="dp-highlighter bg_plain"> <div class="bar"> </div> <ol start="1"> <li class="alt">此处接收被调用方法内部未被捕获的异常 </li> <li>com.zzj.test.reflect.ZeroException: 参数不能小于零! </li> <li class="alt"> at com.zzj.test.reflect.B.run(Reflect.java:13) </li> <li> at com.zzj.test.reflect.Reflect.run(Reflect.java:6) </li> <li class="alt"> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) </li> <li> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) </li> <li class="alt"> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) </li> <li> at java.lang.reflect.Method.invoke(Unknown Source) </li> <li class="alt"> at com.zzj.test.reflect.Test.main(Test.java:11) </li> <li></li>
</ol></div></div>
来源:oschina
链接:https://my.oschina.net/u/4367553/blog/3546321