package com.smbea.demo.reflect;

/**
* 越界异常
* @author hapday
* @date 2017年1月20日 @time下午7:59:01
*/
public class OverstepBoundaryException extends Exception { /**
*
*/
private static final long serialVersionUID = 1L; private String message; public String getMessage() {
return message;
} public OverstepBoundaryException(String message) {
this.message = message;
} } package com.smbea.demo.reflect; public class B {
public void say(int cursor) throws OverstepBoundaryException{
double number [] = new double[5]; for(int index = 0; index < 5; index++) {
number[index] = Math.random();
} if(0 > cursor) {
throw new OverstepBoundaryException("数组索引不可以小于 0!");
}
if(4 < cursor) {
throw new OverstepBoundaryException("数组索引不可以大于 5!");
} System.out.println("cursor = " + cursor + ", number[" + cursor + "] = " + number[cursor]);
}
} package com.smbea.demo.reflect; /**
* 当被调用的方法内部出现了异常,而未被捕获时,将由 InvocationTargetException 异常来接收
* @author hapday
* @date 2017年1月20日 @time下午8:21:04
*/
public class A {
public void print(int length) throws OverstepBoundaryException {
B b = new B();
b.say(length);
}
} package com.smbea.demo.reflect; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; /**
* InvocationTargetException 异常的抛出通常是一个方法调用另一个方法时,都未捕获方法中的异常
* @author hapday
* @date 2017年1月20日 @time下午8:16:50
*/
public class Test {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("com.smbea.demo.reflect.A");
try {
Method method = clazz.getMethod("print", int.class);
try {
method.invoke(clazz.newInstance(), 5);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println("此处接收了方法内部未被捕获的异常。");
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} package com.smbea.demo.reflect; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; /**
* InvocationTargetException 异常的抛出通常是一个方法调用另一个方法时,都未捕获方法中的异常
* @author hapday
* @date 2017年1月20日 @time下午8:16:50
*/
public class Test2 {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("com.smbea.demo.reflect.A");
try {
Method method = clazz.getMethod("print", int.class);
try {
method.invoke(clazz.newInstance(), -1);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println("此处接收了方法内部未被捕获的异常。");
Throwable throwable = e.getTargetException();
throwable.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

  当被调用的方法内部出现了异常而未被捕获时,将由 InvocationTargetException 异常来接收。

最新文章

  1. 【翻译】MongoDB指南/CRUD操作(一)
  2. The first documents
  3. codeforces346 Div.2 A.Round House
  4. 使用Python对Excel表格进行简单的读写操作(xlrd/xlwt)
  5. WPF学习之路(八)页面
  6. 用git上传本地项目到github上
  7. SQL语句查询所耗时间与效能的语句
  8. HTML5塔防游戏——《三国塔防》 - Yorhom&#39;s Game Box
  9. HTTP报文格式
  10. cocos2d-x 2.2.0 如何在lua中注册回调函数给C++
  11. jQuery UI 之 EasyUI 快速入门
  12. Button UI Kit CSS3美丽Buttonbutton
  13. pytest框架之命令行参数1
  14. PB开发境界 多个DW进行update
  15. HDU1846 Brave Game
  16. box-shadow比较美观的阴影
  17. sas通过IMPORT过程读取外部文件数据
  18. Mac上安装pipenv时报错
  19. Spring 学习03
  20. mac系统在配置navicat时连接数据的时候提示can&#39;t connect to mysql server on &#39;127.0.0.1&#39;

热门文章

  1. VUE使用微信JDK(附踩坑记录)
  2. [SDOI2008]烧水问题 规律
  3. Session_Start
  4. 模拟使用zookeeper实现master选举
  5. ORA-14517: Subpartition of index &quot;string.string&quot; is in unusable state
  6. 关于pycharm使用sqlite数据可视化的使用
  7. js最后深入总结
  8. 阿里云 Ubuntu14.04 部署 LAMP
  9. 通过id、classname定位元素,程序仍报找不到元素的原因
  10. netty在rpc MQ中的应用