Catching an exception class by value rather than by reference has several bad effects:

* Slicing occurs, yielding an instance of the exception's base class, rather than the potentially more specific exception class that was actually thrown. This means that only the base class' functions will be available; any additional data or functionality that is offered by the extended class will not be accessible.
* Memory is allocated unnecessarily.
* Copying the exception class might potentially throw an exception.

You might also be tempted to catch an exception by pointer, but this causes issues related to the exception lifetime, and should also be avoided. This situation is detected by rule S1035.


Therefore exception classes should always be caught by reference.


== Noncompliant Code Example

----
try
{
  // ...
}
catch(ExceptionClass ex)
{
  //...
}
----


== Compliant Solution

----
try
{
  // ...
}
catch(ExceptionClass &ex)
{
  //...
}
----


== See

* MISRA {cpp}:2008, 15-3-5 - A class type exception shall always be caught by reference
* https://wiki.sei.cmu.edu/confluence/x/SXs-BQ[CERT, ERR61-CPP.] - Catch exceptions by lvalue reference
* https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e15-catch-exceptions-from-a-hierarchy-by-reference[{cpp} Core Guidelines E.15] - Catch exceptions from a hierarchy by reference

