• InvocationTargetException异常


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

  • 相关阅读:
    使用zabbix监控sql server的发布订阅
    MySQL执行计划
    3.MongoDB恢复探究:为什么oplogReplay参数只设置了日志应用结束时间oplogLimit,而没有设置开始时间?
    面试官看完我手写的单例直接惊呆了!
    故事:坐在我隔壁的小王问我什么是HyperLogLog
    JDK15就要来了,你却还不知道JDK8的新特性!
    不要再问我 in,exists 走不走索引了
    JVM性能调优(4) —— 性能调优工具
    JVM性能调优(3) —— 内存分配和垃圾回收调优
    JVM性能调优(2) —— 垃圾回收器和回收策略
  • 原文地址:https://www.cnblogs.com/hapday/p/6326614.html
Copyright © 2020-2023  润新知