1

I am new to exception handling so i can't understand why in this code if i created an object with the empty parentheses, it doesn't throw the exception object but if i passed parameters to the constructor it works fine. If this has something to do with the type of constructor , can you mention the different types of constructor so i can google them . Thanks in advance.

class test {
public:
    class error {};
    test(){
        throw error();
    }
};

int main()
{
    try {
        test p();
    }
    catch(test::error)
    {
        cout <<"Error caught!"<< endl;
    }
    return 0;
}
  • Are you familiar with the [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse)? – kabanus Dec 29 '19 at 08:55

1 Answers1

1

Vexing parse with test p(); (which is function declaration).

Use test p{}; or test p; instead

Jarod42
  • 203,559
  • 14
  • 181
  • 302