使用异常的最佳实践(Best Practices for Using Exceptions)
来源:互联网 发布:网络十大不文明行为 编辑:程序博客网 时间:2024/05/16 17:52
1. 总是要做一些清理工作(Always clean up after yourself)
如果你使用一些资源例如数据库连接或者网络连接,请记住要做一些清理工作(如关闭数据库连接或者网络连接),如果你的API抛出Unchecked exception,那么你要用try-finally来做必要的清理工作:
public void dataAccessCode(){
Connection conn = null;
try{
conn = getConnection();
..some code that throws SQLException
}catch(SQLException ex){
ex.printStacktrace();
} finally{
DBUtil.closeConnection(conn);
}
}
class DBUtil{
public static void closeConnection
(Connection conn){
try{
conn.close();
} catch(SQLException ex){
logger.error("Cannot close connection");
throw new RuntimeException(ex);
}
}
}
DBUtil是一个工具类来关闭Connection.有必要的说的使用的finally的重要性是不管程序是否碰到异常,它都会被执行。在上边的例子中,finally中关闭连接,如果在关闭连接的时候出现错误就抛出RuntimeException.
2. 不要使用异常来控制流程(Never use exceptions for flow control)
下边代码中,MaximumCountReachedException被用于控制流程:
public void useExceptionsForFlowControl() {
try {
while (true) {
increaseCount();
}
} catch (MaximumCountReachedException ex) {
}
//Continue execution
}
public void increaseCount()
throws MaximumCountReachedException {
if (count >= 5000)
throw new MaximumCountReachedException();
}
上边的useExceptionsForFlowControl()用一个无限循环来增加count直到抛出异常,这种做法并没有说让代码不易读,但是它是程序执行效率降低。
记住,只在要会抛出异常的地方进行异常处理。
3. 不要忽略异常
当有异常被抛出的时候,如果你不想恢复它,那么你要毫不犹豫的将其转换为unchecked exception,而不是用一个空的catch块或者什么也不做来忽略它,以至于从表面来看象是什么也没有发生一样。
4. 不要捕获顶层的Exception
unchecked exception都是RuntimeException的子类,RuntimeException又继承Exception,因此,如果单纯的捕获Exception,那么你同样也捕获了RuntimeException,如下代码:
try{
..
}catch(Exception ex){
}
一旦你写出了上边的代码(注意catch块是空的),它将忽略所有的异常,包括unchecked exception.
5. Log exceptions just once
Logging the same exception stack trace more than once can confuse the programmer examining the stack trace about the original source of exception. So just log it once.
总结
这里给出了一些关于异常处理的一些最佳实践,我并不想开始另一轮的关于checked exception 和 unchecked exception的争论。你可以根据自己的实际情况定制自己异常处理,我坚信我们将有更好的办法来处理我们代码中的异常。
- 使用异常的最佳实践(Best Practices for Using Exceptions)
- 异常处理之最佳实践(Best Practices for Exception Handling )
- 加快您的网站的最佳实践(Best Practices for Speeding Up Your Web Site)
- 最佳实践(Best Practices)
- Web 站点提速的最佳实践(Best Practices for Speeding Up Your Web Site)
- Hibernate最佳实践(Best Practices)
- Hibernate 最佳实践(Best Practices)
- Android后台处理最佳实践(Best Practices for Background Jobs)
- Android后台处理最佳实践(Best Practices for Background Jobs)
- Hibernate 最佳实践(Best Practices)
- jquery 最佳实践 best practices
- Best Practices for Scaling the Windows Forms DataGridView Control(winform中控制DataGridView控件规模的最佳实践)
- 加快您的网站的最佳实践(Best Practices for Speeding Up Your Web Site)
- Android Developers--Best Practices for User Input(第九章) 对用户输入的最佳实践
- mysql集群的最佳实践 Best Practices For MySQL Cluster (文档 ID 1926680.1)
- (翻译中止)Best Practices for Speeding Up Your Web Site 网站加速最佳实践
- Best practices for using KeyHolder?
- 异常处理的哲学Tips for Using Exceptions(1)
- 排序算法:选择排序
- 用PS制作点九图
- 漫谈程序员系列:薪资,你是我不能言说的伤
- java读取word内容
- 基础试题
- 使用异常的最佳实践(Best Practices for Using Exceptions)
- quick 常见bug
- 4、Median of Two Sorted Arrays
- 1060.Are They Equal
- ffmpeg的有关文章
- 我的Shiro学习之旅(配置到功能实现),第二篇
- Mac点击关闭按钮退出程序 与 程序隐藏后点击dock图标重新显示
- 看看
- 极品模特 于小姐粉色内衣居家美女美图