通常,如果要抛出异常,则表明您的代码完全损坏了,并且您对此无能为力。
例如,如果我正在设置数据源:
try {
Class.forName("org.neo4j.jdbc.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
this.dataSource.setDriverClassName("org.neo4j.jdbc.Driver");
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
如果抛出这个异常,显然我需要整理我的代码。
显然我不应该只打印这个堆栈跟踪,可能更好的做法是弹出异常,警告用户出现问题,通知管理员并记录错误。
我的问题是 - 我应该在这里做什么?我只是抛出 ClassNotFoundException 吗?或者我可以将其包装在更通用的“此异常是致命的”异常中吗?
最佳答案
我建议使用 assert
语句。 assert
语句的全部目的是查找并标记错误。
在你的具体情况下,我想你可以这样做:
try {
Class.forName("org.neo4j.jdbc.Driver");
} catch (ClassNotFoundException e1) {
assert (false) : "Class org.neo4j.jdbc.Driver doesn't seem to exist. "
+ "Please find the address of the developer of this program, "
+ "knock on his door, and demand your money back.";
}
(纯属玩笑)
关于java - 有 'general logical error exception'吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31128082/