使用异常的最佳实践(Best Practices for Using Exceptions)
2015-01-22 14:51
656 查看
1. 总是要做一些清理工作(Always
clean up after yourself)
如果你使用一些资源例如数据库连接或者网络连接,请记住要做一些清理工作(如关闭数据库连接或者网络连接),如果你的API抛出Unchecked exception,那么你要用try-finally来做必要的清理工作:
DBUtil是一个工具类来关闭Connection.有必要的说的使用的finally的重要性是不管程序是否碰到异常,它都会被执行。在上边的例子中,finally中关闭连接,如果在关闭连接的时候出现错误就抛出RuntimeException.
2. 不要使用异常来控制流程(Never use exceptions
for flow control)
下边代码中,MaximumCountReachedException被用于控制流程:
上边的useExceptionsForFlowControl()用一个无限循环来增加count直到抛出异常,这种做法并没有说让代码不易读,但是它是程序执行效率降低。
记住,只在要会抛出异常的地方进行异常处理。
3. 不要忽略异常
当有异常被抛出的时候,如果你不想恢复它,那么你要毫不犹豫的将其转换为unchecked exception,而不是用一个空的catch块或者什么也不做来忽略它,以至于从表面来看象是什么也没有发生一样。
4. 不要捕获顶层的Exception
unchecked exception都是RuntimeException的子类,RuntimeException又继承Exception,因此,如果单纯的捕获Exception,那么你同样也捕获了RuntimeException,如下代码:
一旦你写出了上边的代码(注意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的争论。你可以根据自己的实际情况定制自己异常处理,我坚信我们将有更好的办法来处理我们代码中的异常。
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); } } } |
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(); } |
记住,只在要会抛出异常的地方进行异常处理。
3. 不要忽略异常
当有异常被抛出的时候,如果你不想恢复它,那么你要毫不犹豫的将其转换为unchecked exception,而不是用一个空的catch块或者什么也不做来忽略它,以至于从表面来看象是什么也没有发生一样。
4. 不要捕获顶层的Exception
unchecked exception都是RuntimeException的子类,RuntimeException又继承Exception,因此,如果单纯的捕获Exception,那么你同样也捕获了RuntimeException,如下代码:
try{ .. }catch(Exception ex){ } |
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 Exception Handling )
- 加快您的网站的最佳实践(Best Practices for Speeding Up Your Web Site)
- mysql集群的最佳实践 Best Practices For MySQL Cluster (文档 ID 1926680.1)
- CLR Inside Out: Best Practices for Managed and Native Code Interoperability (托管/非托管代码互通性最佳实践)
- WWDC 2016 Session笔记 - Session 213 使用最佳实践改进App(Improving Existing Apps with Modern Best Practices)
- Android后台处理最佳实践(Best Practices for Background Jobs)
- Android后台处理最佳实践(Best Practices for Background Jobs)
- 加快您的网站的最佳实践(Best Practices for Speeding Up Your Web Site)
- Android后台处理最佳实践(Best Practices for Background Jobs)
- Web 站点提速的最佳实践(Best Practices for Speeding Up Your Web Site)
- 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(第九章) 对用户输入的最佳实践
- 网站提速的最佳实践-Best Practices for Speeding Up Your Web Site,来自雅虎开发者网络
- Search Engine Optimization for Flash: Best practices for using Flash on the web
- Atitit. 异常的使用总结最佳实践java .net php Vo8f
- Best Practices for Using ADO.NET
- Best MVC Practices 最佳的MVC实践
- Jenkins最佳实践 - Jenkins Best Practices
- 多网卡下的网络配置方法---- Best Practices for Using Multiple Network Interfaces (NICs)