Example of an Incorrect catch Argument

A catch argument causes an error if it is a value argument, and a copy of it cannot be generated. For example:

class B {
public:
      B();
      B(B&);
};
             // the following catch block will cause an error
             //
catch(const B x)
{
// ...
}

The catch block causes an error because the compiler does not know the type of the object thrown at compile time. It assumes that the type of the thrown object is the same as the type of the catch argument. In the above example, the thrown object is assumed to be of type const B. The compiler uses a copy constructor on the thrown argument to create the catch argument. Because there is no copy constructor for class B that accepts const B as an input argument, the compiler cannot perform the construction and an error occurs.