Bug 284301

Summary: checked exceptions and annotation style
Product: [Tools] AspectJ Reporter: Andrew Clement <aclement>
Component: CompilerAssignee: aspectj inbox <aspectj-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3    
Version: DEVELOPMENT   
Target Milestone: ---   
Hardware: PC   
OS: Windows NT   
Whiteboard:

Description Andrew Clement CLA 2009-07-22 13:06:40 EDT
Was originally attached to bug 37898:

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 error.

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(UndeclaredThrowableException.class));
        }
    }
}
JVM supposed to throw UndeclaredThrowableException in such cases but instead
Exception is thrown. Tested on aspectj version 1.6.2 jdk 1.6.14.
Comment 1 Andrew Clement CLA 2009-07-22 13:12:51 EDT
Here is an old thread from the user list talking about throwing checked exceptions:

http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg01412.html

I would say that if code style does not allow it then it is not allowed and a bug that annotation style lets it through.  This goes for all constructs really - code style is the more mature mechanism.