GeneralPurpose-ThrowByValueCatchByReference

Throw exceptions by value and catch them by reference

Required inputs: IR

Do not use new when throwing exceptions, as it is unclear who would have the responsibility for freeing the exception object. Instead, throw exceptions by value.

Catching exceptions by value will lead to object slicing when the thrown exception inherits from the exception type specific in the catch clause. Catch exceptions by reference to allow for polymorphism.

Example
try
{
    throw MyException(args);
}
catch (const MyException& ex)
{
    ...
}

Possible Messages

Key

Text

Severity

Disabled

catch_without_reference

Exceptions shall be caught by reference.

None

False

throwing_pointer

Exceptions shall be thrown by value.

None

False

Options

only_class_types

only_class_types : bool = True

Whether all types should be caught by reference or only class types. If set to True, non-class types (e.g. primitive types) may be caught by value.