惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
C
Check Point Blog
G
Google Developers Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
D
Docker
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News

博客园 - 叶落无痕

A generic error occurred in gdi+ error的解决方法 Debian 安装完后控制台中文乱码解决 Parser Error Message: Could not load type '_Default'. ubuntu 7.10里装apxs2(Firebird相关) ncurses安装的问题 What is a framework (From C++ FAQ) 可以自定义URL的前缀类型:Asynchronous Pluggable Protocols [zz]Word 2007文档中图片不显示或对象不显示的解决方法 POI_Java里访问Excel,Word等格式的文件 winsock的accept变成不阻塞的可能原因之一 Cstring与BSTR cdecl, stdcall, pascal,fastcall 都有什么区别,具体是什么调用约定? HRESULT相关 directShow调用的COM前要CoInitialize 第一个directShow程序碰到的问题 用directShow要在VS.NET2003设置的目录 CFile的Read出错? 写程序碰到困难一定要调试 55种javascript技巧
return code 与 exception的比较 (From C++ FAQ)
叶落无痕 · 2007-07-17 · via 博客园 - 叶落无痕

What are the disadvantages of using return codes for error handling?

They don't separate exceptional logic from normal logic as well as exceptions do,

they impose avoidable overhead, and they can't be used in constructors.

Return codes are a nice-guy approach; they allow the caller to do something when an error occurs but they don't require the caller to do anything or even notice that the error has occurred.

Return codes require an explicit if-check after every function call. This spreads the error-handling code into every caller of every function rather than focusing it on the relatively few routines that can actually correct the problem. Return codes therefore create a complex chain that is hard to test and maintain everyone percolates the error information backward until finally someone is capable of handling it.

Since testing for return codes requires a conditional branch in the normal execution path, it imposes runtime costs for situations that rarely occur. When functions were hundreds of lines long, checking for return codes was a small percentage of the executable code. But with OO, where member functions often have less than ten lines of code, return codes would impose an unnecessary performance penalty.

Return codes can't be returned from constructors. Fortunately constructors can (and should) throw exceptions. So using return codes with constructors can be disastrous since return codes allow errors to remain uncorrected. For example, if a hash table can't allocate memory for its hash buckets, it might set an error flag within its object, hoping the caller will check this flag and do the right thing. Thus all the object's callers are expected to check this flag (presumably another member function that would have to be added), and all the object's member functions would also have to check the flag. This adds a lot of unnecessary decision logic as well as overhead.