Don’t Ignore That Error!

Don’t Ignore That Error!

Pete Goodliffe

I was walking down the street one evening to meet some friends in a bar. We hadn’t shared a beer in some time, and I was looking forward to seeing them again. In my haste, I wasn’t looking where I was going. I tripped over the edge of a curb and ended up flat on my face. Well, it serves me right for not paying attention, I guess.

It hurt my leg, but I was in a hurry to meet my friends. So, I pulled myself up and carried on. As I walked farther, the pain was getting worse. Although I’d initially dismissed it as shock, I rapidly realized there was something wrong.

But I hurried on to the bar regardless. I was in agony by the time I arrived. I didn’t have a great night out, because I was terribly distracted. In the morning, I went to the doctor and found out I’d fractured my shin bone. Had I stopped when I felt the pain, I would’ve prevented a lot of extra damage that I caused by walking on it. Probably the worst morning after of my life.

TOO MANY PROGRAMMERS write code like my disastrous night out.

Error, what error? It won’t be serious. Honestly. I can ignore it. This is not a win- ning strategy for solid code. In fact, it’s just plain laziness. (The wrong sort.) No matter how unlikely you think an error is in your code, you should always check for it, and always handle it. Every time. You’re not saving time if you don’t; you’re storing up potential problems for the future.

We report errors in our code in a number of ways, including:

? Return codes can be used as the resulting value of a function to mean “it didn’t work.” Error return codes are far too easy to ignore. You won’t see anything in the code to highlight the problem. Indeed, it’s become normal practice to ignore some standard C functions’ return values. How often do you check the return value from printf?

? errno is a curious C aberration, a separate global variable set to sig- nal error. It’s easy to ignore, hard to use, and leads to all sorts of nasty problems—for example, what happens when you have multiple threads

??52 97 Things Every Programmer Should Know

?

calling the same function? Some platforms insulate you from pain here; others do not.

? Exceptions are a more structured language-supported way of signaling and handling errors. And you can’t possibly ignore them. Or can you? I’ve seen lots of code like this:

try {

// …do something…

}

catch (…) {} // ignore errors

The saving grace of this awful construct is that it highlights the fact that you’re doing something morally dubious.

If you ignore an error, turn a blind eye, and pretend that nothing has gone wrong, you run great risks. Just as my leg ended up in a worse state than if I’d stopped walking on it immediately, plowing on regardless of the red flags can lead to very complex failures. Deal with problems at the earliest opportunity. Keep a short account.

Not handling errors leads to:

? Brittle code. Code that’s filled with exciting, hard-to-find bugs.

? Insecure code. Crackers often exploit poor error handling to break into software systems.

? Poor structure. If there are errors from your code that are tedious to deal with continually, you probably have a poor interface. Express it so that the errors are less intrusive and their handling is less onerous.

Just as you should check all potential errors in your code, you need to expose all potentially erroneous conditions in your interfaces. Do not hide them, pre- tending that your services will always work.

Why don’t we check for errors? There are a number of common excuses. Which of these do you agree with? How would you counter each one?

? Error handling clutters up the flow of the code, making it harder to read, and harder to spot the “normal” flow of execution.

? It’s extra work, and I have a deadline looming.

? I know that this function call will never return an error (printf always works, malloc always returns new memory—if it fails, we have bigger problems…).

? It’s only a toy program, and needn’t be written to a production-worthy level.

时间: 2024-10-12 10:33:42

Don’t Ignore That Error!的相关文章

大数据常见错误

1.用./bin/spark-shell启动spark时遇到异常:java.net.BindException: Can't assign requested address: Service 'sparkDriver' failed after 16 retries! 解决方法:add export SPARK_LOCAL_IP="127.0.0.1" to spark-env.sh 2.java Kafka producer error:ERROR kafka.utils.Util

Spark SQL数据加载和保存实战

一:前置知识详解: Spark SQL重要是操作DataFrame,DataFrame本身提供了save和load的操作, Load:可以创建DataFrame, Save:把DataFrame中的数据保存到文件或者说与具体的格式来指明我们要读取的文件的类型以及与具体的格式来指出我们要输出的文件是什么类型. 二:Spark SQL读写数据代码实战: import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaRD

NetApp F3020 盘柜报警升级修复全过程

报警错误: Fri Dec 11 17:00:00 CST [disk.fw.downrevWarning:warning]: 14 disks have downrev firmware that you need to update. Fri Dec 18 15:48:28 CST [raid.disk.offline:notice]: Marking Disk /aggr0/plex0/rg0/0b.27 Shelf 1 Bay 11 [NETAPP   X274_HPYTA146F10 

异常处理最佳实践

一.异常的分类 常规分类: 1.运行时异常(RuntimeException): 2.编译时异常(CheckedException) 用途分类: 1.打断(终止)程序继续往下运行: 2.打断程序继续往下运行,并将异常原因和信息送往上层. 特点分类: 1.可以获得异常的原因: 2.可以获得异常的代号: 3.可以获得异常的错误行号: 4.可以获得异常的堆栈信息(程序运行轨迹): 5.可以获得异常的类型: ……等. 判断分类: 1.可以预判(预先判断).自主定义的异常,比如我们自己写程序,在Servi

采用highchart js+flot+rrd生成cpu、mem状态监控图

HTML <script type="text/javascript" src="../static/js/jquery-1.8.0.min.js"></script>            <script src="../static/js/highcharts.js"></script>            <script language='javascript' src='../..

getSystemservice()原理

1. 说明 android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardService提供剪切板功能,PackageManagerService提供软件包的安装移除及查看等等,应用程序可以通过系统提供的Manager接口来访问这些Service提供的数据,以下将说明他们的工具流程 2. 举例说明基本流程 以android系统支持sensor(传感器)实例来说明框架层的s

Avatar: TCP connection migration with CRIU

Hope we can find Avatar and goal in. Program: simple client/server, loopback Tools: CRIU does checkpoint/restore work ps -ef liuqius+ 20052 2464 0 22:00 pts/6 00:00:00 ./server 7022 liuqius+ 20053 2464 0 22:00 pts/6 00:00:00 ./client 127.0.0.1 7022 W

php--常用的时间处理函数

天地四方曰宇,往古来今曰宙 时间是世界的重要组成部分,不论花开花落,还是云卷云舒都有它的影子. 但它源起何处?又将去向何方?没人知道答案,也不需要答案,我们需要的只是一个相对的起点来标识时间,现今世界普遍采用公元纪年法来表示. 公元纪年法以耶稣诞生日记为公元1年(没有公元0年),中国处于汉平帝刘衎(不会读...)登基第二年即元始元年. 关于时间的另一个概念是unix时间戳,是从1970年1月1日开始所经过的秒数,不考虑闰秒,什么是闰秒参考这里. 下面就来说说php中时间的处理方法,以获取当前时间

Stream 流操作

Stream 类 先看下面的图 Stream 是所有流的抽象基类(不能被实例化,需要使用他的派生类FileStream/MemoryStream/BufferedStream).流是字节序列的抽象概念,例如文件.输入/输出设备.内部进程通信管道或者 TCP/IP 套接字.Stream 类及其派生类提供这些不同类型的输入和输出的一般视图,使程序员不必了解操作系统和基础设备的具体细节. 流涉及三个基本操作: 可以读取流.读取是从流到数据结构(如字节数组)的数据传输. 可以写入流.写入是从数据结构到流