Exception Handling Guidelines & Best Practices

本文概述了从《ObjectDesign:Roles,Responsibilities,andCollaborations》中总结的异常处理核心原则,包括避免处理编码错误、减少异常类声明、在抽象边界重新封装异常等,旨在提高软件响应性和故障诊断效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Let’s review some basic exception design guidelines, summarized from Object Design: Roles, Responsibilities, and Collaborations (Rebecca Wirfs-Brock and Alan McKean, Addison-Wesley, 2003).

Don’t try to handle coding errors.

Unless your software is required to take extraordinary measures in error scenarios, don’t spend a lot of time designing it to detect and recover from programming errors. In the case of an out-of-bounds array index, divide-by zero error, or any other programming error, the best strategy is to fail fast (and leave an audit trail of the problem that can be used to troubleshoot it).

Avoid declaring lots of exception classes.

Create a new exception class only when you expect some handling of the code to take a significantly different action, based on the exception type. In my experience it is rarely the case and exception classes available in java API serve the purpose. Recast lower-level exceptions to higher-level ones whenever you raise an abstraction level.

Don’t let implementation details leak out of a method invocation as exceptions.

Otherwise, your users might think your software is broken. When low-level exceptions percolate up to a high-level handler, there’s little context to assist the handler in making informed decisions or reporting conditions that are traceable to any obvious cause. Recasting an exception whenever you cross an abstraction boundary enables exception handlers higher up in the call chain to make more informed decisions. If you want to include a problem trace when recasting them, you can always create a chained exception. A chained exception provides added context and holds a reference to the original lower level exception. You can repeatedly chain exceptions.

Provide context along with an exception.

What’s most important in exception handling is information that helps create an informed response. Exception classes hold information. You can design them to be packed with information in addition to the bare-bones stack trace information provided by default. You might include values of parameters that raised the exception, specific error text, or detailed information that could be useful to plan a recovery.

Handle exceptions as close to the problem as you can.

As a first line of defense, consider the initial requestor. If the caller knows enough to perform a corrective action, you can rectify the condition on the spot. If you propagate an exception far away from the source, it can be difficult to trace the source. Often objects further away from the problem can’t make meaningful decisions.

Use exceptions only to signal emergencies.

Exceptions shouldn’t be raised to indicate normal branching conditions that will alter the flow in the calling code. For example, a find operation may return zero, one, or many objects, so I wouldn’t raise an exception in this case. Instead, I’d design my find() method to return a null object or an empty collection. A dropped database connection, on the other hand, is a real emergency. There’s nothing that can be done to continue as planned.

Don’t repeatedly re-throw the same exception.

Although exceptions don’t cost anything until they’re raised, programs that frequently raise exceptions run more slowly.

Reference: Exception Handling Guidelines/Best Practices from our JCG partner Sanjeev Kumar at the Architect’s Diary blog.


转自:http://www.javacodegeeks.com/2012/04/exception-handling-guidelines-best.html

内容概要:本文详细解析了2014年全国大学生电子设计竞赛C题——智能小车设计的全过程。文章首先介绍了该竞赛的背景及其重要意义,指出其不仅是对学生电子设计能力的考验,还对学生的学术成长和职业发展有深远影响。随后,文章深入剖析了C题的具体要求,包括小车的起跑、行驶、超车等复杂动作,强调了硬件(如控制模块、电源模块、车体、电机模块)和软件(如信号检测与控制、两车通信、节能技术、程序设计)方面的关键技术和实现方法。最后,文章分享了测试与优化的经验,并总结了团队合作、知识储备和实践能力的重要性,展望了电子设计领域的发展趋势。 适合人群:电子信息类专业学生、电子设计爱好者及希望深入了解智能小车设计的技术人员。 使用场景及目标:①了解全国大学生电子设计竞赛的背景和重要性;②掌握智能小车设计的硬件选型和软件编程技巧;③学习信号检测与控制、两车通信、节能技术等关键技术;④借鉴测试与优化的经验,提升实际动手能力和解决问题的能力。 阅读建议:本文内容详实,涵盖了从理论到实践的各个方面。建议读者在阅读过程中结合实际操作,逐步理解和掌握智能小车设计的各项技术和原理,特别是对硬件电路设计和软件编程部分,可以通过搭建实验平台进行实践,加深理解。同时,关注文中提到的测试与优化策略,有助于提高实际项目的成功率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值