Announcement Announcement Module
Collapse
No announcement yet.
AOP Exception Handling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP Exception Handling

    I am new to AOP and have been trying to create an AOP-based exception handling framework for my application. I know this question has been asked in these forums before, but I have not been able to determine why exceptions thrown in the code are not being logged. Essentially, I am just trying to log all exceptions that are thrown in the com.foo.bar package.

    Configuration:
    Code:
    <bean id="errorHandler" class="com.foo.bar.arch.error.ErrorHandler"/>
    <aop:aspectj-autoproxy/>
    <aop:config>
    	<aop:after-throwing
    			pointcut="within(com.foo.bar..*)"
    			throwing="exception" method="processError"/>
    	</aop:aspect>
    </aop:config>
    ErrorHandler:
    Code:
    public final class ErrorHandler
    {
        ...
        public void processError(JoinPoint logged, MyException exception)
        {
            logger.error(exception.getCause().getMessage());
        } 
        ... 
    }
    In order to test this, I am simply throwing a MyException in my test code:
    Code:
    public void testAOPErrorHandling()
    {
            try
            {
                throw new MyException("something");
            }
            catch(MyException e)
            {
                // do something
            }
    }
    Am I incorrect in catching the exception here? My understanding was that it would still be logged.

    Thanks!

  • #2
    1. Do you programm to interfaces, if not enable proxy-target-class
    2. Why do you also use aspectj autoproxing? You use aop blocks to configure?

    Comment


    • #3
      1. The class that is throwing the error is an implementation of an interface. Enabling proxy-target-class did not help (although it did alert me that several methods could not be logged because they are final - not the test class though).

      2. The AspectJ auto-proxying was a mistake, I picked it up somewhere in my attempts to figure out what was is wrong.

      Any other ideas?

      Thanks

      Comment


      • #4
        Code:
        <aop:config>
        	<aop:after-throwing
        			pointcut="within(com.foo.bar..*)"
        			throwing="exception" method="processError"/>
        	</aop:aspect>
        </aop:config>
        Ehrm is that your actual configuration?! Because if that is so I would expect your application to blow up because your xml is invalid.

        Also is the bean you are using (the one that throws the exception) configured in your application context if not spring isn't going to proxy anything, it can only intercept calls to object it itself manages, not objects created with new...

        Comment


        • #5
          Bad copy and paste on my part, the configuration is actually:

          Code:
          <aop:config>
              <aop:aspect ref="errorHandler">
          	<aop:after-throwing
          		pointcut="within(com.foo.bar..*)"
          		throwing="exception" method="processError"/>
              </aop:aspect>
          </aop:config>
          The bean that throws the exception is created by Spring - it is created as part of a JUnit test class and Spring injects an instance of the bean via AbstractTransactionalDataSourceSpringContextTests.

          Thanks

          Comment


          • #6
            Aha...I found my problem. The exception I was throwing never crossed the proxy so it was not picked up by my error handler. I changed my test code to throw a runtime exception, instead of catching it inline and it worked.

            Thanks for the help

            Comment


            • #7
              doh.... That I missed that, complete missed your try/catch block.

              Comment

              Working...
              X