无法从 main 中捕获异常:after() throwing(Exception e) - AspectJ
Can't catch exception from main in: after() throwing(Exception e) - AspectJ
我正在尝试捕获在我的 Java class.
中抛出的异常
我的主要代码:
public static void main(String[] args){
new something();
throw new RuntimeException();
}
就我而言,我创建了 after() returning: execution(* main(*)) { advice}
和 after() throwing(Exception e): execution(* main(*)) { advice }
来确定是否在 main 中抛出异常,以便在每个建议中做不同的事情。
注意 在第二个里面,我在输出中打印 e
异常使用:
System.out.println("Threw an exception: " + e + "Joinpoint: " + thisJoinPoint.getSignature().toString());
问题是,即使我在 main 中抛出异常,并且从输出中可以看出匹配的切入点是第二个(输出:Threw an exception: java.lang.RuntimeExceptionJoinpoint: void main(String[])
),我仍然会收到此错误在我的输出中:
Exception in thread "main" java.lang.RuntimeException
at main(C.java:24)
所以,据我了解,我没有捕捉到异常,我只是确定在 main 中发生了异常。
有没有一种方法可以在不使用 around()
的情况下捕获此异常?
您无法使用 after() throwing
建议抑制异常,您需要使用您怀疑的 around()
建议。
void around(): execution(* MainClass.main(*)) {
try {
proceed();
} catch (Exception e) {
//suppress
System.out.println("Suppressed an exception: "
+ e + "Joinpoint: " + thisJoinPoint.getSignature().toString());
}
}
当在您感兴趣的某个点抛出异常时,after() throwing
建议对 运行 附加代码有益,但它不会阻止异常传播 ,除非你从你的建议代码中抛出另一个异常(如果你这样做,包装被抑制的异常):
after() throwing(Exception e): execution(* MainClass.main(*)) {
System.out.println("Threw an exception: " + e + "Joinpoint: "
+ thisJoinPoint.getSignature().toString());
throw new RuntimeException("Another exception", e);
}
编辑:我正在添加一个关于如何模拟 before()
、after() returning
、after() throwing
和 after()
的示例around() advice
在对我的回答发表评论时跟进一个问题的建议。
void around(): execution(* MainClass.main(*)) {
try {
//before() code
System.out.println("Before main started.");
proceed();
//after() returning code
System.out.println("Main exited normally.");
} catch (Exception e) {
//after() throwing code suppressing exception unless you rethrow it
System.out.println("Suppressed an exception: " + e + "Joinpoint: "
+ thisJoinPoint.getSignature().toString());
} finally {
//after() code
System.out.println("After main executed.");
}
}
当你的主 class 是 运行 时,这将输出以下行:
Before main started.
Main started.
Suppressed an exception: java.lang.RuntimeException: errorJoinpoint: void MainClass.main(String[])
After main executed
请注意,after() returning
部分的代码不会执行,因为您的主要 class 不会正常完成,因为它会抛出异常,就像正常的 after() returning
在这种情况下建议不会 运行。
我正在尝试捕获在我的 Java class.
中抛出的异常我的主要代码:
public static void main(String[] args){
new something();
throw new RuntimeException();
}
就我而言,我创建了 after() returning: execution(* main(*)) { advice}
和 after() throwing(Exception e): execution(* main(*)) { advice }
来确定是否在 main 中抛出异常,以便在每个建议中做不同的事情。
注意 在第二个里面,我在输出中打印 e
异常使用:
System.out.println("Threw an exception: " + e + "Joinpoint: " + thisJoinPoint.getSignature().toString());
问题是,即使我在 main 中抛出异常,并且从输出中可以看出匹配的切入点是第二个(输出:Threw an exception: java.lang.RuntimeExceptionJoinpoint: void main(String[])
),我仍然会收到此错误在我的输出中:
Exception in thread "main" java.lang.RuntimeException
at main(C.java:24)
所以,据我了解,我没有捕捉到异常,我只是确定在 main 中发生了异常。
有没有一种方法可以在不使用 around()
的情况下捕获此异常?
您无法使用 after() throwing
建议抑制异常,您需要使用您怀疑的 around()
建议。
void around(): execution(* MainClass.main(*)) {
try {
proceed();
} catch (Exception e) {
//suppress
System.out.println("Suppressed an exception: "
+ e + "Joinpoint: " + thisJoinPoint.getSignature().toString());
}
}
当在您感兴趣的某个点抛出异常时,after() throwing
建议对 运行 附加代码有益,但它不会阻止异常传播 ,除非你从你的建议代码中抛出另一个异常(如果你这样做,包装被抑制的异常):
after() throwing(Exception e): execution(* MainClass.main(*)) {
System.out.println("Threw an exception: " + e + "Joinpoint: "
+ thisJoinPoint.getSignature().toString());
throw new RuntimeException("Another exception", e);
}
编辑:我正在添加一个关于如何模拟 before()
、after() returning
、after() throwing
和 after()
的示例around() advice
在对我的回答发表评论时跟进一个问题的建议。
void around(): execution(* MainClass.main(*)) {
try {
//before() code
System.out.println("Before main started.");
proceed();
//after() returning code
System.out.println("Main exited normally.");
} catch (Exception e) {
//after() throwing code suppressing exception unless you rethrow it
System.out.println("Suppressed an exception: " + e + "Joinpoint: "
+ thisJoinPoint.getSignature().toString());
} finally {
//after() code
System.out.println("After main executed.");
}
}
当你的主 class 是 运行 时,这将输出以下行:
Before main started.
Main started.
Suppressed an exception: java.lang.RuntimeException: errorJoinpoint: void MainClass.main(String[])
After main executed
请注意,after() returning
部分的代码不会执行,因为您的主要 class 不会正常完成,因为它会抛出异常,就像正常的 after() returning
在这种情况下建议不会 运行。