I would like to ensure that, **whenever a class that throws an exception is constructed, there is a try-catch block to capture the exception right there **. In the example, I would like to get a warning from compiler when I create A
object in the statement auto a = A(3);
suggesting to add a try-catch block right there.
There is always freedom in C++ to decide where to capture exceptions. Is there any mechanism to ensure that the exception is caught at construction time? My final purpose is to ensure that by mistake, the exception of A
is not caught somewhere else.
#include <stdexcept>
struct A
{
A(int v) noexcept(false)
{
if (v < 10)
throw std::invalid_argument( "Value is too small" );
value = v;
}
private:
int value;
};
int main()
{
auto a = A(3);
}