AspectJ:我可以中和“扔”吗? (用log替换)并继续该方法
在下面的代码中,我想中和抛出并继续该方法 - 可以做到吗?
public class TestChild extends TestParent{
private String s;
public void doit(String arg) throws Exception {
if(arg == null) {
Exception e = new Exception("exception");
throw e;
}
s=arg;
}
最终
结果应该是,在触发异常的情况下 (arg == null)
- throw e 被 Log(e) 替换
- s=arg 被执行
谢谢
PS:我可以“吞掉”异常或用另一个异常替换它但在所有情况下,该方法都不会继续,我的所有干预都是在损害发生时发生的(即抛出异常)
In below code I want to neutralize the throw and continue the method - Can it be done ?
public class TestChild extends TestParent{
private String s;
public void doit(String arg) throws Exception {
if(arg == null) {
Exception e = new Exception("exception");
throw e;
}
s=arg;
}
}
The net result should be that, in case of the exception triggered (arg == null)
- throw e is replaced by Log(e)
- s=arg is executed
Thanks
PS : I can 'swallow' the exception or replace it with another exception but in all cases the method does not continue, all my interventions take place when the harm is done (ie the exception has been thrown)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强烈怀疑是否存在通用解决方案。但对于您的特定代码和要求 1 和 2:
}
I strongly doubt that general solution exists. But for your particular code and requirements 1 and 2:
}
我认为,在大多数情况下,您想要的通常没有意义,因为如果应用程序抛出异常,它就有理由这样做,并且该原因几乎总是包括不继续正常控制流的意图由于虚假数据可能导致后续错误而引发异常的方法。例如,如果您可以中和代码中的
throw
,并且下一行代码将执行如下操作:您明白我的意思了吗?假设你可以,继续在这里控制流程,你会冒不可估量的风险。现在有什么替代方案?
null
值在这里不会造成任何损害。那么为什么不直接用after() throwing
建议捕获异常呢?null
有害并且您知道这一点,为什么不拦截方法执行并覆盖参数以避免异常呢?around()
建议并从那里调用proceed() 使用不同的参数值(例如某些身份验证令牌或密码)多次,直到被调用的方法不再抛出异常。
正如您所看到的,有很多方法可以解决您的实际问题,具体取决于问题的具体内容以及您想要实现的目标。
说了这么多,现在让我们回到最初的技术问题,即不捕获,但实际上中和异常,即以某种方式避免它的存在根本就扔了。因为 AspectJ 语言不包含执行您想要的操作的技术手段(感谢上帝!),所以您可以查看其他可以以更底层的方式操作 Java 类文件的工具。我从未有效地使用过它们,但我很确定您可以使用 BCEL 或Javassist。
I think that generally what you want makes no sense most cases because if an application throws an exception it has a reason to do so, and that reason almost always includes the intention not to continue with the normal control flow of the method where the exception was thrown due to possible subsequent errors caused by bogus data. For example, what if you could neutralise the
throw
in your code and the next lines of code would do something like this:Do you get my point? You take incalculable risks by continuing control flow here, assuming you can at all. Now what are the alternatives?
null
does not do any harm here. Then why not just catch the exception with anafter() throwing
advice?null
is harmful and you know about it, why not intercept method execution and overwrite the parameter so as to avoid the exception to begin with?around()
advice and from there callproceed()
multiple times with different argument values (e.g. some authentication token or password) until the called method does not throw an exception anymore.As you see, there are many ways to solve your practical problem depending on what exactly the problem is and what you want to achieve.
Having said all this, now let us return to your initial technical question of not catching, but actually neutralising an exception, i.e. somehow avoiding its being thrown at all. Because the AspectJ language does not contain technical means to do what you want (thank God!), you can look at other tools which can manipulate Java class files in a more low-level fashion. I have never used them productively, but I am pretty sure that you can do what you want using BCEL or Javassist.