英文:
Is it possible to automatically call an internal exception method when the exception is thrown?
问题
每当特定自定义异常被抛出时,我需要执行一个方法。执行的代码将把异常报告给我的API。异常类具有一个boolean
参数,指示是否应该报告异常。让我们假设我有以下异常类的代码:
public MyException extends Exception {
private final String message;
private final int code;
private final boolean report;
public MyException(String message, int code, boolean report) {
this.message = message;
this.code = code;
this.report = report;
}
public void report() {
if(report) {
// 报告一些内容
}
}
}
当抛出MyException
时,我希望report()
方法中的代码被执行。我已经考虑过手动调用该方法:
try {
throw new MyException("Test", 1, true);
} catch(MyException e) {
e.report();
}
但我想知道是否可能在异常抛出时自动调用该函数:
...
throw new MyException("Test", 1, true); // 隐式调用report()
...
请注意,我不想在异常实例化时调用它,因为可能会发生这样的情况:
...
public MyException(String message, int code, boolean report) {
this.message = message;
this.code = code;
this.report = report;
report();
}
...
int var = 0;
MyException ex = new MyException("test", 1, true);
if (var != 0) {
throw ex;
}
// 这里会报告异常但不会抛出它。
这是否可能?第三方库是否能够实现这一点?任何帮助都将不胜感激!
英文:
I need to execute a method every time a certain custom exception is thrown. The executed code will report the exception to my API. The exception class has a boolean
parameter that states if the exception should be reported or not. Let's assume I have the following exception coded:
public MyException extends Exception {
private final String message;
private final int code;
private final boolean report;
public MyException(String message, int code, boolean report) {
this.message = message;
this.code = code;
this.report = report;
}
public void report() {
if(report) {
// Report some stuff
}
}
}
When MyException
is thrown I want the code in report()
to be executed. I have already thought of calling the method manually:
try {
throw new MyException("Test", 1, true);
} catch(MyException e) {
e.report();
}
But I was wondering if It was possible to call the function automatically on an exception throw.
...
throw new MyExcepion("Test", 1, true); // Implicitlly calls report()
...
Note that I do not want to call it on exception instantiation because something like this could happen:
...
public MyException(String message, int code, boolean report) {
this.message = message;
this.code = code;
this.report = report;
report();
}
...
int var = 0;
MyException ex = new MyException("test", 1, true);
if (var != 0) {
throw ex;
}
// Here the exception would be reported but never thrown.
Is this even possible? Can 3rd party library do this? Any help is appreciated!
答案1
得分: 1
Spring AspectJ可能是答案,如果已经有Spring - 请参考示例https://howtodoinjava.com/spring-aop/aspectj-afterthrowing-annotation-example/。
英文:
Spring AspectJ might be answer if already have spring - see example https://howtodoinjava.com/spring-aop/aspectj-afterthrowing-annotation-example/
答案2
得分: 1
你可以使用和 UncaughtExceptionHandler。
使用模式如下:
- 创建你自己的
UncaughtExceptionHandler
,它只是一个方法的实现:uncaughtException()
- 在每个线程或线程组上使用 setDefaultUncaughtExceptionHandler 注册你的处理程序。
如果你正在使用任何框架,如Java EE或Spring,可能有更好(更清洁)的方法,如Java EE ExceptionMapper。
英文:
You can use and UncaughtExceptionHandler.
The usage pattern would be:
- Create your own
UncaughtExceptionHandler
, which is just an implementation of one method:uncaughtException()
- Register your handler on each Thread or ThreadGroup using setDefaultUncaughtExceptionHandler
If you are using any framework, such as Java EE or Spring, there might be better (cleaner) methods, such as Java EE ExceptionMapper.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论