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.

I also do not use exceptions in C++ anymore

4 pointsby NotThe1Pctover 1 year ago

3 comments

qa_accover 1 year ago
Hi, professional C++ developer here, I find this post casually and, for curiosity, I watched the video. The example used by the guy is wrong beyond recovery and I&#x27;m about to detail why after the premise that, yes, exception are slow:<p>1. exceptions have that name because are intended to handle event out of normal , error and <i>not</i> to returns value or information to the caller. No skilled C++ developer will write functions like those, never !<p>2. In that kind of function exception are useless, in fact that should be write like this, with exception disabled:<p>int calc(int a, int b) noexcept {<p>if(... omissis ...) return 1; return 0;<p>}<p>3. In case the function had some more complex errors, the correct form for the use of exceptions should be:<p>int calc(int a, int b) {<p>try{<p>&#x2F;&#x2F; More complex logic here<p>if(... omissis ...) return 1; return 0;<p>} catch(myexcept&amp; ex){ &#x2F;&#x2F; do something &#x2F;&#x2F; possibly other catch for different except here } catch(...){ &#x2F;&#x2F; do something }<p>}<p>So, not to return information but for error handling.<p>3. From a skilled developer , from a financial company, I expected a real life example , with correct exception use and a real case in which their presence can slow down a critical section of a program. That could be an interesting lesson;<p>4. Moreover, the examples are particularly bad because could induce people to think that in other contexts, where performance are not so important, the use of the exceptions in that way is justifiable ! I guess that could be a reason the article was downvoted.<p>5. I hate &quot;dogma driven programming&quot;, IMHO to demonise whole class of features of a language without critical sense is wrong: yes most application has critical sections that are performance sensitive but, also frequently, large portions of them are not. So why make programmer&#x27;s life miserable banning without think features like exceptions or polymorphism &quot;because is written&quot;? Moreover, if some little parts is justified that are written even in Assembly, maybe large chunks could be written in Go or Python without influence on the general performance of the system.
fithisuxover 1 year ago
Exceptions seem to be a half baked thing. Now that I learnt about the algebraic effects I am convinced. This is the real thing.<p>Until it finds its way into mainstream, I prefer the C&#x2F;Golang approach.
NotThe1Pctover 1 year ago
The cost of exceptions is a typical server platform is so high that even very small chances of it happening is enough to throw a low latency application off its rails.