You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AfterThrowingTest.java 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import org.aspectj.lang.JoinPoint;
  2. import org.aspectj.lang.annotation.AfterThrowing;
  3. import org.aspectj.lang.annotation.Aspect;
  4. @Aspect
  5. public class AfterThrowingTest {
  6. public static void main(String[] args) {
  7. try {
  8. new B().start();
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. // include "JoinPoint" in the argument list
  14. @AfterThrowing(pointcut = "execution(public void B.start())", throwing = "ex")
  15. public void handleExceptionJP(JoinPoint jp, Exception ex) {
  16. }
  17. // include "JoinPoint.StaticPart" in the argument list
  18. @AfterThrowing(pointcut = "execution(public void B.start())", throwing = "ex")
  19. public void handleExceptionJPSP(JoinPoint.StaticPart jp, Exception ex) {
  20. }
  21. // include "JoinPoint.EnclosingStaticPart" in the argument list
  22. @AfterThrowing(pointcut = "execution(public void B.start())", throwing = "ex")
  23. public void handleExceptionJPESP(JoinPoint.EnclosingStaticPart jp, Exception ex) {
  24. }
  25. // include "JoinPoint" and "JoinPoint.EnclosingStaticPart" in the argument list
  26. @AfterThrowing(pointcut = "execution(public void B.start())", throwing = "ex")
  27. public void handleExceptionJPESP(JoinPoint jp1, JoinPoint.EnclosingStaticPart jp, Exception ex) {
  28. }
  29. // make sure it still works if "JoinPoint" is second on the argument list
  30. @AfterThrowing(pointcut = "execution(public void B.start())", throwing = "ex")
  31. public void handleExceptionJP2(JoinPoint jp, Exception ex) {
  32. }
  33. }
  34. class B implements I {
  35. public void start() throws Exception {
  36. throw new IllegalArgumentException();
  37. }
  38. }
  39. interface I {
  40. public void start() throws Exception;
  41. }