1. A catch statement should never catch throwable since it includes errors. 在catch里永远不要抛出throwable。
Explain: Catching Throwable errors is not recommended since its scope is very broad. It includes runtime issues such as OutOfMemoryError that should be exposed and managed separately.在catch里抛出Throwable里不被推荐的,因为它的范围极其广泛,这个包含了一些远行时异常,例如:OutOfMemoryError,内容溢出,而这些错误应该被单独管理。
Code:
public void bar() {
try {
// do something
} catch (Throwable th) { // should not catch Throwable
th.printStackTrace();
}
}
2. 不要使用sysout或者e.printStackTrace(),打印出信息,应该使用统一的logger进行日志、异常输出。
3. 对于不需要被其他类继承应该定义成final来控制其不被继承性
4. 工具类不应该有公共的构造器