I found out that it is possible to throw checked exception that is missing in signature of a method. It is possible only via annotation style aspect declaration. If you try to do same via .aj files you'll get compiler exception.
I wonder is it a bug or a feature?
Here is test case.
/** A class that doesn't declare that it throws any checked exception */ public class Foo { public void foo() { } } /**An aspect that throws Exception each time method Foo is called */
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect;
@Aspect public class FooAspect { @Around("execution(void Foo.foo())")
public Object throwException(ProceedingJoinPoint pjp) throws Throwable { throw new Exception(); } }
/**Tests that undeclared check exception was thrown*/ import org.junit.Assert; import org.junit.Test;
public class TextExceptionHandling {
@Test public void undeclaredCheckedExceotionIsThrown() {
try { new Foo().foo(); Assert.fail(); } catch (Exception e) { Assert.assertTrue(e.getClass().equals(Exception.class)); } } }
PS I suppose that it is a bug since JVM suppose to throw UndeclaredThrowableException in this case.