Rethrowing the caught exception variable slices derived types

ID

c.correctness.preserve_stack_trace

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Error Handling

Language

C / C++

Description

This handler catches an exception into a named variable and rethrows that same variable. throw $V; constructs a new exception object of the variable’s static (declared) type, so if the object actually caught was a derived type, its extra state is sliced away and any handler further up the stack that expects the derived type will no longer see it. Use a bare throw; to rethrow the original exception object unchanged.

Rationale

This handler catches an exception into a named variable and rethrows that same variable. throw $V; constructs a new exception object of the variable’s static (declared) type, so if the object actually caught was a derived type, its extra state is sliced away and any handler further up the stack that expects the derived type will no longer see it. Use a bare throw; to rethrow the original exception object unchanged.

The following code illustrates the pattern detected by this rule:

void badByValue() {
  try {
    risky();
  } catch (std::exception e) {
    log(e.what());
    // FLAGGED: Rethrowing the caught exception variable slices derived types
    throw e;
  }
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved