When a constructor/destructor has a function-try-block, the code inside of the catch clause will be executed after the object has been destroyed (if the object was partially constructed when the exception was thrown, this part will be destroyed before going in the catch block). Therefore, the members of the object are not available, and it is undefined behavior to access them.


Since the lifetime of a static member is greater than that of the object itself, so a static member can be accessed from the catch code.


== Noncompliant Code Example

----
class A {
public:
  int i;
  A ( ) try {
    // Action that might raise an exception
  } catch ( ... ) {
    if ( i == 0 ) { // Noncompliant, i has been destroyed
      // ...
    }
  }
  ~A ( ) try {
    // Action that might raise an exception
  } catch ( ... ) {
    if ( i == 0 ) { // Noncompliant
      // ...
    }
  }
};
----


== See

* MISRA {cpp}:2008, 15-3-3
* https://wiki.sei.cmu.edu/confluence/x/_3s-BQ[CERT, ERR53-CPP.] - Do not reference base classes or class data members in a constructor or destructor function-try-block handler



ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
