Why catch an exception as reference-to-const?

c++, constants, exception

Solution

You need:

- a reference so you can access the exception polymorphically

- a const to increase performance, and tell the compiler you're not going to modify the object

The latter is not as much important as the former, but the only real reason to drop const would be to signal that you want to do changes to the exception (usually useful only if you want to rethrow it with added context into a higher level).

Problem

I've heard and read many times that it is better to catch an exception as reference-to-const rather than as reference. Why is: ``` try { // stuff } catch (const std::exception& e) { // stuff } ``` better than: ``` try { // stuff } catch (std::exception& e) { // stuff } ```

Original source

Related problems