Partager via


Compiler Error C2316 

Error Message

'exception' : cannot be caught as the destructor and/or copy constructor are inaccessible

An exception was caught by value or by reference but the copy constructor and/or the assignment operator were inaccessible.

This code was accepted by the previous version's compiler but now gives an error. For more information, see Summary of Compile-Time Breaking Changes.

Example

The following sample generates C2316:

// C2316.cpp
// compile with: /EHsc
#include <stdio.h>

extern "C" int printf_s(const char*, ...);

struct B 
{
public:
    B() {}
    // Delete the following line to resolve.
private:
    // copy constructor
    B(const B&) 
    {
    }
};

void f(const B&) 
{
}

int main() 
{
    try 
    {
        B aB;
        f(aB);
    }
    catch (B b) {   // C2316
        printf_s("Caught an exception!\n");   
    }
}