methodtroublereturns.java

来自「JAVA 工作指南 可以说是程序员必备的东西哦」· Java 代码 · 共 26 行

JAVA
26
字号
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class MethodTroubleReturns {    private void drinkMe(int liters) {	if (liters < 0)	    throw new IllegalArgumentException("I can't drink a negative amount of liquid");    }    public static void main(String... args) {	try {	    MethodTroubleReturns mtr  = new MethodTroubleReturns(); 	    Class<?> c = mtr.getClass();   	    Method m = c.getDeclaredMethod("drinkMe", int.class);	    m.invoke(mtr, -1);        // production code should handle these exceptions more gracefully	} catch (InvocationTargetException x) {	    Throwable cause = x.getCause();	    System.err.format("drinkMe() failed: %s%n", cause.getMessage());	} catch (Exception x) {	    x.printStackTrace();	}    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?