When throw
is followed by an expression, this expression will be used to create and initialize the exception object. In other words,
the exception object is copy-initialized from the expression.
catch (const std::exception& ex) {
// ...
throw ex; // "throw" copy-initializes the exception object from "ex"
}
Rethrowing an unmodified copy of the caught exception is a bad practice because:
- The exception already exists, so it is better to reuse it instead of creating a new one and wasting resources.
- The copy will be an instance of the exception base class rather than the potentially more specific exception class initially caught. So it may
lead to a loss of precision.