### Question:
ExceptionMethod implements Implementation, ByteCodeAppender { public static Implementation throwing(Class<? extends Throwable> throwableType) { return throwing(TypeDescription.ForLoadedType.of(throwableType)); } ExceptionMethod(ConstructionDelegate constructionDelegate); static Implementation throwing(Class<? extends Throwable> throwableType); static Implementation throwing(TypeDescription throwableType); static Implementation throwing(Class<? extends Throwable> throwableType, String message); static Implementation throwing(TypeDescription throwableType, String message); InstrumentedType prepare(InstrumentedType instrumentedType); ByteCodeAppender appender(Target implementationTarget); Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod);  }### Answer:
@Test public void testWithoutMessage() throws Exception { DynamicType.Loaded<Foo> loaded = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(ExceptionMethod.throwing(RuntimeException.class)) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0)); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1)); assertThat(loaded.getLoaded().getDeclaredFields().length, is(0)); Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(Foo.class))); assertThat(instance, instanceOf(Foo.class)); try { instance.foo(); fail(); } catch (RuntimeException exception) { assertThat(exception.getClass(), CoreMatchers.<Class<?>>is(RuntimeException.class)); assertThat(exception.getMessage(), nullValue()); } instance.assertZeroCalls(); }
@Test public void testWithMessage() throws Exception { DynamicType.Loaded<Foo> loaded = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(ExceptionMethod.throwing(RuntimeException.class, BAR)) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0)); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1)); assertThat(loaded.getLoaded().getDeclaredFields().length, is(0)); Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(Foo.class))); assertThat(instance, instanceOf(Foo.class)); try { instance.foo(); fail(); } catch (RuntimeException exception) { assertThat(exception.getClass(), CoreMatchers.<Class<?>>is(RuntimeException.class)); assertThat(exception.getMessage(), is(BAR)); } instance.assertZeroCalls(); }
@Test public void testWithNonDeclaredCheckedException() throws Exception { DynamicType.Loaded<Foo> loaded = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(ExceptionMethod.throwing(Exception.class)) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0)); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1)); assertThat(loaded.getLoaded().getDeclaredFields().length, is(0)); Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(Foo.class))); assertThat(instance, instanceOf(Foo.class)); try { instance.foo(); fail(); } catch (Exception exception) { assertThat(exception.getClass(), CoreMatchers.<Class<?>>is(Exception.class)); assertThat(exception.getMessage(), nullValue()); } instance.assertZeroCalls(); }