CodeQL documentation

Exception thrown in destructor

ID: cpp/throw-in-destructor
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
   - reliability
   - readability
   - language-features
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds exceptions thrown in destructors. This is dangerous: If the destructor is called during stack unwinding as part of the handling of another exception, throwing an additional exception will immediately terminate the program as per the C++ specification.


Handle the error condition in a different way.


class C {
		if (error) {
			throw "Exception in destructor"; //wrong: exception thrown in destructor

void f() {
	C* c = new C();
	try {
		delete c;
	} catch ( char * do_operation_exception) {
		delete c; //would immediately terminate program if C::~C throws an exception


  • © GitHub, Inc.
  • Terms
  • Privacy