May 29
Most programmers know that throwing exceptions takes a relatively long time to execute versus normal processing. Some say throwing exceptions is as much as two orders of magnitude slower than passing arguments (that’s 100 times slower for non-geeks). However, another article claims there’s almost no impact to throwing exceptions, unless you are running your software in a debugger.
I’ve done some further investigations into this. I think the heart of the issue is error checking vs exception handling, IMHO that is.
I hope you take a gander and add your thoughts.
http://www.pchenry.com/Home/tabid/36/EntryID/123/Default.aspx
I think if you decide to blog about a topic it’s reasonable to expect you to know it well (research it to achieve this if required), not just say “some claim it’s slow, some say it’s not”.
If you think about what actually happens at the low level, it’s quite obvious that throwing exceptions involves considerable overhead – especially if the catch is many stack frames below the one throwing one. However, proctecting code in a try-block doesn’t cost anything as long as exceptions don’t occur. So basically we should catch them when we can’t handle them, and otherwise not.
Many programmers write code with lots of catch blocks and few if any throw statements, and then fail to actually *handle* the exceptions in the catch blocks. I’ve even come across quite a few empty catch blocks, which of course serves no purpose other than hide the fact that things went haywire, causing other things to go haywire elsewhere and making it exponentially more difficult to figure out what happened – or, in especially unlucky cases, in no errors the system is aware of at all, but rather corrupted data and false results. It’ great for financial reporting applications…
@Dag: I think if you decide to comment on a blog about a topic it’s reasonable to expect you to know it well (research it to achieve this if required), not just say “involves considerable overhead”.
Very funny, guys. Apparently you did not read the rest of my article. The very next paragraph says:
“In my ad-hoc testing using C#, I discovered that there is some truth to both of the statements above. Outside the debugger, throwing exceptions was on average 6 times slower than passing arguments. But inside the debugger, throwing exceptions was on average 65 times slower than passing arguments. This was a looping test throwing an exception only one level deep, so real-life performance of exceptions multiple levels deep and scattered across a program would be even slower.”
http://www.devtopics.com/exceptions-are-for-exceptions