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

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
A
About on SuperTechFans
Vercel News
Vercel News
B
Blog
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
D
Docker
V
Visual Studio Blog
博客园 - 叶小钗
The Cloudflare Blog
Jina AI
Jina AI
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏

博客园 - 叶落无痕

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.