views:

2412

answers:

8

In general, I tend to use try/catch for code which has multiple failure points for which the failures have a common handler.

In my experience, this is typically code which qualifies input or context before performing some action or output after performing some action.

I have received counsel from literature and colleagues to minimize the code in such blocks and I accept that as generally good advice.

I would like to understand a bit more about the foundation for the above advice:

  • What is the nature of the overhead?
  • Are there recent development guidelines that address the recommended usage (or avoidance) of try/catch blocks?
  • How much do faster processors and more modern compilers mitigate the problems with try/catch?

Thanks in advance for the help,

AJ

A: 

In my experience the biggest problem with try/catch blocks are we often try to catch exceptions too generically. For instance, if I wrap my main function with a try/catch block that catches (...) I am basically trying to not allow my program to crash b/c of a thrown exception.

The problem with this approach as I see it is two fold. 1) While I'm testing and debugging I don't see any errors and I don't get the opportunity to fix them. 2) It's really kind of taking the lazy way out. Instead of thinking through the problems that may come up and figuring out what the edge cases are I'm just trying not to fail. Trying not to fail is a lot different than trying to succeed.

cmaxo
You can always catch and re-throw. Note If an exception escapes main() it is implemention defined wether the stack is unwound (so destructors may not be called). Thus I always catch(...) and log in main and re-throw so that I get the windows error message.
Martin York
+4  A: 

To your second question: general guidelines are here, Herb Sutter also gives pretty good advise here.

Nikolai N Fetissov
+2  A: 

I found a paper by Bjarne Stroustrup (pdf warning) on C++ performance that includes a section on exceptions. You might find it interesting. I've had coworkers who believed there was overhead on every instruction within a try/catch block, but Stroustrup's paper doesn't seem to support that idea.

Fred Larson
A: 

In most languages entering and exiting a try/catch block via the normal methods is free, it's only when an exception is thrown that the exception handler looks up where to handle the exception.

Douglas Leeder
+1  A: 

Depends on a compiler. Why don't you write a simple function with a try-catch block and a similar one without it and compare the generated machine code?

Nemanja Trifunovic
+1  A: 

I find the C++ FAQs site and corresponding book has an enlightening discussion on the matter.

http://www.parashift.com/c++-faq-lite/exceptions.html

Jherico
+7  A: 

In c++, the cost depends on the implementation. In general, there are two ways to implement exceptions:

The first is the "table" approach. The compiler builds a set of tables to look up, at the point where the exception is thrown, where to go. When the exception is thrown, it has to search through each table up the call stack until it finds something that will catch this exception. Since this is all runtime based, entering or exiting a try catch produces no penalty (good) but throwing an exception involves potentially many lookups, producing a much slower throw. I personally prefer the not-having-to-pay for try catch blocks, because exceptions should be a very rare circumstance. This also would make executables larger, if they have to store the tables.

The seconds is the "code" approach. Each time the code enters a try catch block, conceptually, the location of the block is pushed onto a stack. This produces a cost during entering and exiting a try-catch block, however, when an exception is thrown, the runtime mechanism can quickly pop off the stack to find where to go. So, throwing exceptions is (much?) faster, but entering a block now has a cost. Putting a try catch block in a tight low level loop could produce significant overhead.

You would have to check your specific compiler to see which one they use.

Todd Gardner
A: 

In C++ you shouldn't use try/catch blocks to perform cleanup. Instead, you might want to use templates to perform resource aquisition.

auto_ptr's are one [bad] example

Synchronization locks, where you store a mutex as a state variable, and use local variable (templates or regular classes) to perform the .acquire()/.release() methods.

The more of this you do, the less you have to worry about manually releasing objects in exceptional conditions. The C++ compiler will do it for you.

Chris Kaminski