InertiaRSS Track and read blogs, news, and tech you care about
Read Original Open in InertiaRSS

Recommended Feeds

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Latest news
Latest news
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
Malwarebytes
Malwarebytes
G
GRAHAM CLULEY
P
Privacy International News Feed
Spread Privacy
Spread Privacy
S
Schneier on Security
V
V2EX
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
B
Blog RSS Feed
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
S
Securelist
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
Schneier on Security
Schneier on Security
MyScale Blog
MyScale Blog
李成银的技术随笔
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
I
Intezer
Vercel News
Vercel News
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fox-IT International blog
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
Cyberwarzone
Cyberwarzone
博客园 - Franky
S
Secure Thoughts
L
LINUX DO - 热门话题
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
T
Threatpost
博客园 - 司徒正美

阮一峰的网络日志

科技爱好者周刊(第 397 期):财富正在向 AI 集中 科技爱好者周刊(第 397 期):财富正在向 AI 集中 科技爱好者周刊(第 396 期):互联网通信的替代方案 科技爱好者周刊(第 396 期):互联网通信的替代方案 - 阮一峰的网络日志 科技爱好者周刊(第 395 期):软件开发的第三种方式 科技爱好者周刊(第 395 期):软件开发的第三种方式 - 阮一峰的网络日志 科技爱好者周刊(第 394 期):第二次 API 开放浪潮 科技爱好者周刊(第 393 期):脑腐状态 科技爱好者周刊(第 392 期):axios 投毒与好莱坞式骗术 科技爱好者周刊(第 391 期):AI 的贫富分化 科技爱好者周刊(第 390 期):没有语料,大模型就是智障 套壳中国大模型撑起500亿美元估值?扒一扒 Cursor 的"套壳"疑云 科技爱好者周刊(第 389 期):未来如何招聘程序员 科技爱好者周刊(第 388 期):测试是新的护城河 零安装的"云养虾":ArkClaw 使用指南 科技爱好者周刊(第 387 期):你是领先的 科技爱好者周刊(第 386 期):当外卖员接入 AI 字节全家桶 Seed 2.0 + TRAE 玩转 Skill 科技爱好者周刊(第 385 期):马斯克害怕中国车企吗? 智谱旗舰 GLM-5 实测:对比 Opus 4.6 和 GPT-5.3-Codex 科技爱好者周刊(第 384 期):为什么软件股下跌 科技爱好者周刊(第 383 期):你是第几级 AI 编程 Kimi 的一体化,Manus 的分层 科技爱好者周刊(第 382 期):独立软件的黄昏 AI native Workspace 也许是智能体的下一阶段 科技爱好者周刊(第 381 期):中国 AI 大模型领导者在想什么 科技爱好者周刊(第 380 期):为什么人们拥抱"不对称收益" 科技爱好者周刊(第 379 期):《硅谷钢铁侠》摘录 我如何用 AI 处理历史遗留代码:MiniMax M2.1 升级体验 科技爱好者周刊(第 378 期):预测是新的互联网热点 科技爱好者周刊(第 377 期):14万美元的贫困线 科技爱好者周刊(第 376 期):太空数据中心的争议 科技爱好者周刊(第 375 期):一扇门的 Bug 终于有人做了 Subagent,TRAE 国内版 SOLO 模式来了 科技爱好者周刊(第 374 期):6GHz 的问题 VS Code 使用国产大模型 MiniMax M2 教程 科技爱好者周刊(第 373 期):数据模型是新产品的核心 国产大模型接入 Claude Code 教程:以 Doubao-Seed-Code 为例 科技爱好者周刊(第 372 期):软件界面如何设计 大模型比拼:MiniMax M2 vs GLM 4.6 vs Claude Sonnet 4.5 科技爱好者周刊(第 371 期):一个乐观主义者的专访 科技爱好者周刊(第 370 期):正确的代码高亮 科技爱好者周刊(第 369 期):Tim 与罗永浩的对谈 科技爱好者周刊(第 368 期):不要这样管理软件团队 一天之内,智谱和 Anthropic 都发了最强编程模型 科技爱好者周刊(第 367 期):Nano Banana 的几个妙用 科技爱好者周刊(第 366 期):旧金山疯狂的 AI 广告 科技爱好者周刊(第 365 期):流量变现正在崩塌 科技爱好者周刊(第 364 期):最难还原的魔方 科技爱好者周刊(第 363 期):最好懂的神经网络解释
Error handling is better than status codes
阮一峰 · 2025-10-22 · via 阮一峰的网络日志

Error handling can be done in different ways.

JavaScript and Python throw exceptions, while Rust is essentially throwing exceptions.

C and Go languages return an error value, and you must determine whether the value is -1 or null.

I've always wondered which way is better.

Not long ago, I read an article from many years ago.ArticleExplicitly proposeThrowing exceptions is better than returning status codesHis reasoning is very convincing, and it seems the article hasn't been translated into Chinese yet, so I translated it out.

Exceptions and Return Status Codes

Author: Ned Batchelder

Original website URL:nedbatchelder.com

In software, there are two ways to handle errors: throwing exceptions and returning status codes.

Almost everyone agrees that exceptions are a better way to handle errors, but some people still prefer returning status codes. This article explains why exceptions are a better choice.

I. Clean Code

Exceptions allow you to omit error-handling steps in most of your code. They automatically propagate upward through layers that do not catch exceptions. As a result, you can write code without any error-handling logic, which helps keep the code clean and readable.

Let's compare the two ways of writing the same simple function.

First, returning status codes.


STATUS DoSomething(int a, int b)
{
    STATUS st;
    st = DoThing1(a);
    if (st != SGOOD) return st;
    st = DoThing2(b);
    if (st != SGOOD) return st;
    return SGOOD;
}

In the example above, you must check DoThing1(a) andDoThing2(b) normal to proceed to the next step. If

throws an exception, there's no need for intermediate error checks.


void DoSomething(int a, int b)
{
    DoThing1(a);
    DoThing2(b);
}

This is just the simplest case. If you encounter complex scenarios, the noise from status codes will be more severe, while exceptions can keep the code clean.

II. Meaningful return values

Status codes consume valuable return values, forcing you to add code to determine if the return value is correct.

Some functions only need to return a normal value but now have to include error return cases. Over time, the codebase grows, functions become larger, and return values become more complex.

For example, many functions have overloaded return values: "return NULL if failed," or return -1 on failure. The result is that every time you call this method, you have to check if the return value is NULL or -1. If the function later adds new error return values, you must update all call points.

If an exception is thrown, the function only returns when it is successful, and all error handling can be simplified to one place.

III. More Rich Error Information

Status codes are usually integers, and the information they can convey is quite limited. Suppose an error is a file not found, then which file? The status code cannot convey that much information.

When returning a status code, it is best to log an error message in a dedicated error log, so the caller can obtain detailed information from it.

Exceptions are completely different; they are instances of classes, so they can carry a lot of information. Since exceptions can be subclassed, different exceptions can carry different data, forming a very rich error message system.

IV. Can Handle Implicit Code

Some functions cannot return a status code. For example, constructors do not have an explicit return value, so they cannot return a status code. There are also some functions (such as destructors) that cannot even be called directly, let alone return values.

Functions without return values, if not using exception handling, force you to come up with other methods to convey error messages or pretend these functions will never fail. Simple functions might achieve fault tolerance, but the codebase will grow continuously, and the likelihood of failure will also increase. Without a way to express failure, the system will only become more error-prone and harder to understand.

V. Visibility of Errors

Consider what happens if a programmer is negligent and fails to write error handling code.

If the return status code is not checked, the error will go undetected, and the code will continue to execute as if the operation succeeded. The code may fail later, but it might be after many steps of operation. How can you trace the issue back to where the initial error occurred?

On the other hand, if an exception is not caught immediately, it will propagate up the call stack, either reaching a higher catch block or reaching the top level and being handled by the operating system, which typically presents the error to the user. This is bad for the program, but at least the error is visible. You will see the exception, be able to determine where it was thrown, and where it should be caught, thus allowing you to fix the code.

This does not discuss the situation where errors are not reported. In this case, neither returning a status code nor throwing an exception is useful.

So, for errors that are reported but not handled, there are two possibilities: one is that the return status code hides the issue, and the other is that throwing an exception makes the error visible. Which one would you choose?

VI. Rebuttal

Famous programmer Joel Spolsky believes that returning status codes is better, because he thinks exceptions are a much worse form of goto statement.

"Exceptions are invisible in the source code. When reading code blocks, it's impossible to know which exceptions might be thrown and from where. This means that even with careful code review, potential errors cannot be discovered."

"An exception is a function creating too many possible exits. To write correct code, you must consider every possible code path. Each time you call a function that might throw an exception without immediately catching it, the function may suddenly terminate, or other unexpected code paths may occur."

These words sound quite reasonable, but if you change it to return status codes, you must explicitly check every possible return point of the function. So,You have traded explicit complexity for implicit complexity.This also has drawbacks; explicit complexity can make you see the trees but not the forest, and the code will become chaotic as a result.

When facing this explicit complexity, programmers get frustrated and either hide error handling with custom methods or simply omit it.

The former hides error handling, only changing explicit handling back to implicit handling, and is less convenient and feature-complete than the original Try method. The latter omits error handling even worse, as programmers assume some error will not occur, thus埋下风险.

VII. Conclusion

Returning status codes are difficult to use and cannot be used in some places. It hijacks the return value. Programmers may easily fail to write error handling code, causing silent failures in the system.

Exceptions are better than status codes. As long as your programming language provides exception handling tools, use them.

(End)