How to re-throw exception in AspectJ around advise

后端 未结 3 1380
孤城傲影
孤城傲影 2021-02-09 10:19

I have some methods which throws some exception, and I want to use AspectJ around advise to calculate the execution time and if some exception is thrown and to log into error lo

3条回答
  •  野性不改
    2021-02-09 10:43

    I'm afraid you cannot write advice to throw exceptions that aren't declared to be thrown at the matched join point. Per: http://www.eclipse.org/aspectj/doc/released/progguide/semantics-advice.html : "An advice declaration must include a throws clause listing the checked exceptions the body may throw. This list of checked exceptions must be compatible with each target join point of the advice, or an error is signalled by the compiler."

    There has been discussion on the aspectj mailing list about improving this situation - see threads like this: http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg01412.html

    but basically what you will need to do is different advice for each variant of exception declaration. For example:

    Object around() throws ResumeServiceException, ResumeNotFoundException, TException: 
      call (* Iface.* (..) throws ResumeServiceException, ResumeNotFoundException, TException) {
    

    that will advise everywhere that has those 3 exceptions.

提交回复
热议问题