Is there a cost to try catch blocks?

22 pointsposted a day ago
by gsky

3 Comments

slowcache

a day ago

I didn't think that this was much of a thing that needed to be investigated. Adding additional processing into your code will make it slow down.

On a related note, I had a consultant come into my work one time to the teach us some pragmatic things in C++. One of the lessons was that that gcc will not attempt to reorder the code within a try block to optimize it. This could be leading to some very minute slowdown in a C++ app in addition to the overhead associated with try/catch as a whole

zamalek

17 hours ago

For dotnet exceptions are expensive where they are caught, at least if you're looking for the largest effect: it's the unwind.

As for smaller effects that matter in really hot code: try/catch also disallows a method from being inlined. Throw disallowed inlining when I wrote System.HashCode, but this was considered a bug and I'm not sure if it has been fixed - the workaround was to use utility throw methods.

stargrazer

a day ago

Took a few mind shifts to get around what the commenter said vs what exists beyond the link.

The link actually goes to an article with some code snippets which aren't c++. Going to the repository, seems to be .net/c#/f#.

So... for the c++ readers, this isn't c++ related :-)