C++ catch blocks - catch exception by value or reference?
Unless you want to fiddle with the exception, you should usually use a const reference: catch (const CustomException& e) { ... }
. The compiler deals with the thrown object's lifetime.
The standard practice for exceptions in C++ is ...
Throw by value, catch by reference
Catching by value is problematic in the face of inheritance hierarchies. Suppose for your example that there is another type MyException
which inherits from CustomException
and overrides items like an error code. If a MyException
type was thrown your catch block would cause it to be converted to a CustomException
instance which would cause the error code to change.
Catching by value will slice the exception object if the exception is of a derived type to the type which you catch.
This may or may not matter for the logic in your catch block, but there is little reason not to catch by const reference.
Note that if you throw;
without a parameter in a catch block, the original exception is rethrown whether or not you caught a sliced copy or a reference to the exception object.