Throwable.initCause()
的调用,其中异常构造函数还包含 Throwable cause
实参。
在本例中,可以移除 initCause()
调用,并将其实参添加到对异常构造函数的调用中。
示例:
try {
process();
}
catch (RuntimeException ex) {
RuntimeException wrapper = new RuntimeException("Error while processing");
wrapper.initCause(ex); // 不必要调用 'Throwable.initCause()'
throw wrapper;
}
可通过快速修复将 cause 实参传递给构造函数。 在应用快速修复后:
try {
process();
}
catch (RuntimeException ex) {
RuntimeException wrapper = new RuntimeException("Error while processing", ex);
throw wrapper;
}