try{int x = 0;
cin>> x;
if ( x > 100)
throw 1;
else
cout<< “Good data”<< endl;
}
catch (int i)
{
cout<< “Exception “<< i << “thrown”<< endl;
}
I need to know why the catch is int i & not int x?
try{int x = 0;
cin>> x;
if ( x > 100)
throw 1;
else
cout<< “Good data”<< endl;
}
catch (int i)
{
cout<< “Exception “<< i << “thrown”<< endl;
}
I need to know why the catch is int i & not int x?
I need to know why the catch is int i & not int x?
Because i is the 'name' of the exception thrown, and x is the variable name used to store your input data. You could call your exception e or something else, but it shouldn't be the same as the variable you use to store the user's input.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.