本文整理匯總了Java中java.lang.invoke.MethodHandles.throwException方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.throwException方法的具體用法?Java MethodHandles.throwException怎麽用?Java MethodHandles.throwException使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.throwException方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testThrowException
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
countTest();
Class<? extends Throwable> exType = thrown.getClass();
MethodHandle target = MethodHandles.throwException(returnType, exType);
//System.out.println("throwing with "+target+" : "+thrown);
MethodType expectedType = MethodType.methodType(returnType, exType);
assertEquals(expectedType, target.type());
target = target.asType(target.type().generic());
Throwable caught = null;
try {
Object res = target.invokeExact((Object) thrown);
fail("got "+res+" instead of throwing "+thrown);
} catch (Throwable ex) {
if (ex != thrown) {
if (ex instanceof Error) throw (Error)ex;
if (ex instanceof RuntimeException) throw (RuntimeException)ex;
}
caught = ex;
}
assertSame(thrown, caught);
}
示例2: throwException
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle throwException(final Class<?> returnType, final Class<? extends Throwable> exType) {
final MethodHandle mh = MethodHandles.throwException(returnType, exType);
return debug(mh, "throwException", returnType, exType);
}