您的代码不可执行,请在下次提供MCVE 或至少提供完整方面。您甚至不提供建议的签名。这不是就 SO 提出问题的好方法。作为拥有 1,000 多个声望点的用户,您应该知道这一点。
不管怎样,你的问题的答案其实很简单。假设你有这个示例代码:
异常类+驱动应用:
package de.scrum_master.app;
public class MySpecificException extends Exception {
private static final long serialVersionUID = 1L;
}
package de.scrum_master.app;
import java.io.IOException;
public class Application {
public String doSomething(Integer number) {
return number.toString();
}
public void doSomethingElse(boolean doThrow) throws MySpecificException {
if (doThrow)
throw new MySpecificException();
}
public void doWhatever(boolean doThrow) throws MySpecificException, IOException {
if (doThrow)
throw new MySpecificException();
else
throw new IOException();
}
public static void main(String[] args) throws MySpecificException, IOException {
Application application = new Application();
// Just so as not to mess up the console output in the IDE for this demo
System.setErr(System.out);
// No exceptions here
application.doSomething(11);
application.doSomethingElse(false);
// Let's catch some exceptions
try {
application.doSomethingElse(true);
} catch (MySpecificException e) {
System.out.println("Caught " + e);
}
try {
application.doWhatever(true);
} catch (MySpecificException e) {
System.out.println("Caught " + e);
}
try {
application.doWhatever(false);
} catch (IOException e) {
System.out.println("Caught " + e);
}
// Do not catch this one
application.doSomethingElse(true);
}
}
没有方面的控制台日志:
Caught de.scrum_master.app.MySpecificException
Caught de.scrum_master.app.MySpecificException
Caught java.io.IOException
Exception in thread "main" de.scrum_master.app.MySpecificException
at de.scrum_master.app.Application.doSomethingElse(Application.java:12)
at de.scrum_master.app.Application.main(Application.java:50)
这里没有惊喜。我们已经捕获和未捕获的不同类型的异常。现在我们希望有一个方面专门记录所有出现的MySpecificException,无论它们是否被捕获。
方面:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import de.scrum_master.app.MySpecificException;
@Aspect
public class ExceptionLogger {
@AfterThrowing(value = "(execution(* *.*(..)))", throwing = "mySpecificException")
public void logException(JoinPoint thisJoinPoint, MySpecificException mySpecificException) {
System.out.println(thisJoinPoint + " -> " + mySpecificException);
}
}
看到通知方法的签名了吗?只需将异常参数限制为所需的类型即可。
带有方面的控制台日志:
execution(void de.scrum_master.app.Application.doSomethingElse(boolean)) -> de.scrum_master.app.MySpecificException
Caught de.scrum_master.app.MySpecificException
execution(void de.scrum_master.app.Application.doWhatever(boolean)) -> de.scrum_master.app.MySpecificException
Caught de.scrum_master.app.MySpecificException
Caught java.io.IOException
execution(void de.scrum_master.app.Application.doSomethingElse(boolean)) -> de.scrum_master.app.MySpecificException
execution(void de.scrum_master.app.Application.main(String[])) -> de.scrum_master.app.MySpecificException
Exception in thread "main" de.scrum_master.app.MySpecificException
at de.scrum_master.app.Application.doSomethingElse(Application.java:12)
at de.scrum_master.app.Application.main(Application.java:50)