Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of try/catch blocks in C++

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

like image 246
AJ S. Avatar asked Jun 04 '09 15:06

AJ S.


People also ask

What is the use of try catch block?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

What is the use of try catch in C #?

The C# try and catch keywords are used to define a try catch block. A try catch block is placed around code that could throw an exception. If an exception is thrown, this try catch block will handle the exception to ensure that the application does not cause an unhandled exception, user error, or crash the application.

What happens if try catch block is not used?

Java try block is used to enclose the code that might throw an exception. It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.


1 Answers

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.

like image 63
Todd Gardner Avatar answered Sep 23 '22 04:09

Todd Gardner