Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 18.1.2
Analysis Type: Decidable,Single Translation Unit
An empty throw is a throw-expression with no operand.
For the purposes of this rule, the body of a lambda declared within the compound-statement of a catch handler is not considered
to be part of the catch handler.
An empty throw re-throws the temporary object that represents an exception. Its use is intended to enable the handling of an exception to
be split across two or more handlers.
Syntactically, there is nothing to prevent an empty throw from being used outside a catch handler. However, this would result in
implementation-defined program termination when there is no exception object to re-throw.
void f1( void )
throw std::range_error( "range error" );
catch ( std::exception const & )
log( "Caught in f1" );
throw; // Compliant - re-throws an exception object
void f2( void )
throw; // Non-compliant - not syntactically within a catch
Copyright The MISRA Consortium Limited © 2023
S1039 targets the same defect as this rule but for a non-mission-critical context.