博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
InvocationTargetException异常
阅读量:4678 次
发布时间:2019-06-09

本文共 3241 字,大约阅读时间需要 10 分钟。

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 异常来接收。

转载于:https://www.cnblogs.com/hapday/p/6326614.html

你可能感兴趣的文章
sql 查询目标数据库中所有的表以其关键信息
查看>>
C# 高效率创建字符串类(StringBuilder)
查看>>
sql server 符号函数sign
查看>>
bzoj 4337 树的同构
查看>>
OPENQUERY用法以及使用需要注意的地方
查看>>
1001. Extending MyPoint class
查看>>
js使用showModalDialog,弹出一个自适应大小窗口
查看>>
[poj 3436]最大流+输出结果每条边流量
查看>>
webpack的安装
查看>>
字符流Reader和Writer
查看>>
【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器
查看>>
Java替代C语言的可能性
查看>>
android ListView中CheckBox错位的解决
查看>>
linux下的mongodb数据库原生操作
查看>>
BNUOJ 1268 PIGS
查看>>
菜鸟的MySQL学习笔记(三)
查看>>
商业选址5A法则
查看>>
POJ 1191 棋盘分割(区间DP)题解
查看>>
文件同步服务器,iis 集群 ,代码同步(一)
查看>>
JS之模板技术(aui / artTemplate)
查看>>