I realize why it is (usually) best to catch exceptions by reference versus by pointer or by value, and I know that the compiler is able to make some optimizations, in general, if the programmer uses const
variables versus non-const
variables. This Stack Overflow question sheds some light on that subject.
But what that question does not mention is if there are any possible performance optimizations that could be used by catching exceptions by const
reference versus just simply by reference, e.g. using
try
{
...
}
catch (const std::exception& e)
{
...
}
versus just
try
{
...
}
catch (std::exception& e)
{
...
}
I know that there are some things you can do if you catch the exception by non-const
reference, such as modify it and rethrow it, or pass it to a function that takes in a non-const
reference as an argument. I also understand that using const
variables where appropriate generally makes your code more readable. But are there any possible optimizations that the compiler could perform that would make using const
references to exceptions inherently "better" or faster? Or would code generally function exactly the same if I caught all of my exceptions by non-const
reference?