TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Real cost of C++ exception vs error-code checking

71 pointsby zeugmaabout 14 years ago

6 comments

pilifabout 14 years ago
I assume he was proving a point where main() of his full program using error code checking was not in fact checking the return value of foo()<p>Even in simple example code like this you can forget a check. In this case that result would be undefined if any call to devide failed.<p>I'd much rather have my program blow up with a readable stack trace pointing to where it happened than it working with a basically random value and then maybe blowing up somewhere totally unrelated or worse, destroying user data.
评论 #2302173 未加载
McPabout 14 years ago
I prefer Raymond Chen's take on exceptions: <a href="http://blogs.msdn.com/b/oldnewthing/archive/2005/01/14/352949.aspx" rel="nofollow">http://blogs.msdn.com/b/oldnewthing/archive/2005/01/14/35294...</a>
评论 #2301933 未加载
评论 #2301908 未加载
评论 #2301896 未加载
Quarrelsomeabout 14 years ago
Nice article but as far as I'm concerned you don't need to _prove_ this as it is a logical fallacy to start with.<p>If an exception is being thrown then something is wrong, if something isn't wrong then you implemented your exceptions incorrectly as exceptions shouldn't exist in normal program flow.<p>So to recap, you're writing a crap ton of more code just so you can return your error code _slightly_ faster than it would take an exception. You're optimising your failure cases, which (in the _vast_ majority of cases) is UTTERLY ABSURD.
评论 #2302111 未加载
评论 #2302763 未加载
评论 #2302088 未加载
JoeAltmaierabout 14 years ago
Perhaps the worst problem with checking-vs-exceptions is, either solution dominates your code structure, obscuring the algorithm logic.<p>The holy grail would be some method of ensuring the code cannot fail e.g. weirdly constrained argument semantics. Thus separating algorithm from constraints instead of shuffling them together on the page like a deck of cards.
评论 #2303578 未加载
评论 #2304360 未加载
AshleysBrainabout 14 years ago
Today with terabyte harddrives, gigabytes of RAM and broadband connections, when is the binary size a more important factor than both execution speed and ease of development? Especially when the binary size difference is probably not huge?<p>Shouldn't the advice of this article just be "use exceptions"?
评论 #2301979 未加载
评论 #2301898 未加载
评论 #2302436 未加载
评论 #2302585 未加载
评论 #2302414 未加载
评论 #2302181 未加载
评论 #2301944 未加载
gershabout 14 years ago
A few issues. Does he actually benchmark? Things don't always work in practice the way you would think.<p>If you are going for ultra-high performance, do you even have error-checking? Do you write it in assembler?