Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong.
Use Exceptions Rather Than Return Codes
Separate the normal operations with error handlings.
e.g.
Bad code:
public class DeviceController {
...
public void sendShutDown() {
DeviceHandle handle = getHandle(DEV1);
// Check the state of the device
if (handle != DeviceHandle.INVALID) {
// Save the device status to the record field
retrieveDeviceRecord(handle);
// If not suspended, shut down
if (record.getStatus() != DEVICE_SUSPENDED) {
pauseDevice(handle);
clearDeviceWorkQueue(handle);
closeDevice(handle);
} else {
logger.log("Device suspended. Unable to shut down");
}
} else {
logger.log("Invalid handle for: " + DEV1.toString());
}
}
...
}
Good code:
public class DeviceController {
...
public void sendShutDown() {
try {
tryToShutDown();
} catch (DeviceShutDownError e) {
logger.log(e);
}
}
private void tryToShutDown() throws DeviceShutDownError {
DeviceHandle handle = getHandle(DEV1);
DeviceRecord record = retrieveDeviceRecord(handle);
pauseDevice(handle);
clearDeviceWorkQueue(handle);
closeDevice(handle);
}
private DeviceHandle getHandle(DeviceID id) {
...
throw new DeviceShutDownError("Invalid handle for: " + id.toString());
...
}
...
}
Write Your Try-Catch-Finally Statement First
It is good practice to start with a try-catch-finally statement when you are writing code that could throw exceptions.
(本节中有关单元测试的讲解,没看明白,留待以后回顾再看。)
Use Unchecked Exceptions
Checked exceptions is an Open/Closed Principle violation.
(C# doesn't have checked exceptions.)
Provide Context with Exceptions
To determine the source and location of an error.
Mention the operation that failed and the type of failure.
Define Exception Classes In terms of a Caller's Needs
Most important concern: how they are caught.
In most exception handling situations, the work that we do is relatively standard regardless of the actual cause. So we can simplify our code considerably by wrapping the third-party APIs.
e.g.
Bad code:
ACMEPort port = new ACMEPort(12);
try {
port.open();
} catch (DeviceResponseException e) {
reportPortError(e);
logger.log("Device response exception", e);
} catch (ATM1212UnlockedException e) {
reportPortError(e);
logger.log("Unlock exception", e);
} catch (GMXError e) {
reportPortError(e);
logger.log("Device response exception");
}
finally {
…
}
Good code:
LocalPort port = new LocalPort(12);
try {
port.open();
}
catch (PortDeviceFailure e) {
reportError(e);
logger.log(e.getMessage(), e);
}
finally {
…
} public class LocalPort {
private ACMEPort innerPort;
public LocalPort(int portNumber) {
innerPort = new ACMEPort(portNumber);
}
public void open() {
try {
innerPort.open();
} catch (DeviceResponseException e) {
throw new PortDeviceFailure(e);
} catch (ATM1212UnlockedException e) {
throw new PortDeviceFailure(e);
} catch (GMXError e) {
throw new PortDeviceFailure(e);
}
}
…
}
Define the Normal Flow
Use the Special Case Pattern. Create a class or configure an object so that it handles a special case for you. When you do, the client code doesn't have to deal with exceptional behavior. That behavior is encapsulated in the special case object.
Don't Return Null
When we return null, we are essentially creating work for ourselves and foisting problems upon our callers.
If you are tempted to return null from a method, consider throwing an exception or returning a special case object instead. If you are calling a null-returning method from a third-party API, consider wrapping that method with a method that either throws an exception or returns a special case object.
e.g.
Bad code:
List<Employee> employees = getEmployees();
if (employees != null) {
for(Employee e : employees) {
totalPay += e.getPay();
}
}
Good code:
List<Employee> employees = getEmployees();
for(Employee e : employees) {
totalPay += e.getPay();
} public List<Employee> getEmployees() {
if( .. there are no employees .. )
return Collections.emptyList();
}
Don't Pass Null
Conclusion
We can write robust clean code if we see error handling as a separate concern, something that is viewable independently of our main logic. To the degree that we are able to do that, we can reason about it independently, and we can make great strides in the maintainability of our code.
Clean Code–Chapter 7 Error Handling的更多相关文章
- Clean Code – Chapter 3: Functions
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...
- Clean Code – Chapter 4: Comments
“Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...
- TIJ——Chapter Twelve:Error Handling with Exception
Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...
- Clean Code – Chapter 2: Meaningful Names
Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...
- Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...
- Clean Code – Chapter 5 Formatting
The Purpose of Formatting Code formatting is about communication, and communication is the professio ...
- setjmp()、longjmp() Linux Exception Handling/Error Handling、no-local goto
目录 . 应用场景 . Use Case Code Analysis . 和setjmp.longjmp有关的glibc and eglibc 2.5, 2.7, 2.13 - Buffer Over ...
- Erlang error handling
Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...
- Error Handling
Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...
随机推荐
- win7环境下配置Java环境
==下载Java SE Development Kit 8u45== http://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...
- UIViewCotroller 的生命周期函数
Viewcontroller 的所有生命周期函数 重写时 一定要先写 父类 方法 就是(super +生命周期函数) LoadView ViewDidLoad ViewDidUnload: 在iOS ...
- SGU481 Hero of Our Time
Description Saratov ACM ICPC teams have a tradition to come together on Halloween and recollect terr ...
- Matlab实现K-Means聚类算法
人生如戏!!!! 一.理论准备 聚类算法,不是分类算法.分类算法是给一个数据,然后判断这个数据属于已分好的类中的具体哪一类.聚类算法是给一大堆原始数据,然后通过算法将其中具有相似特征的数据聚为一类. ...
- csuoj 1351: Tree Counting
这是一个动态规划的题: 当初想到要用dp,但是一直想不到状态转移的方程: 题解上的原话: 动态规划,设 g[i]表示总结点数为 i 的方案种数,另设 f[i][j]表示各个孩子的总结点数为i,孩子的个 ...
- c++学习之旅-Cygwin+Eclipse ide for c++
一,cygwin下载完毕后配置系统环境片两path指向cygwin/bin 二,eclipse设置 2.1 设置工作目录的cygwin映射 cygwin/d ->d:\ 2.2设置编译 下面新建 ...
- loadrunner http协议put模式脚本编写
web_submit_data("rest", "Action=http://www.test.com/111ojhjh.do", "Method=P ...
- Eclipse 安装Activiti 插件失败解决方法
遇到的错误为:1.4.0' but it could not be found等.
- [jobdu]包含min函数的栈
老题,两个stack.其中一个维护min值就行了. #include <iostream> #include <stack> using namespace std; int ...
- [wikioi]均分纸牌
这是一道归为贪心题...http://wikioi.com/problem/1098/ 参考:http://www.cnblogs.com/taoziwel/articles/1859984.html ...