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 異常來接收。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM