exceptions are only caught if they are thrown first. No part of your code throws any exception using the throw
keyword. But even if you do that your catch
block has to be in the loop as well so you have to exit loop using break
statement if that's your intention
You can do the following
void Addition(float num)
{
int Sum=0;
cout<<"Please enter number you wish to add:"<<endl;
cout<<"Please enter number or enter -9999 to exit"<<endl;
while( true ) // whcih actually makes it infinite
{
try{
cin>>num;
if(num == -9999)
throw -9999; // you can throw any value in this case
Sum+=num;
}
catch(...)
{
cout <<" ABORTING.."<<endl;
break;
}
}
cout << "Sum is:" << Sum << endl;
}
The above code is quite unnecessary it could have done simply like this
void Addition(float num)
{
int Sum=0;
cout<<"Please enter number you wish to add:"<<endl;
cout<<"Please enter number or enter -9999 to exit"<<endl;
while( true ) // whcih actually makes it infinite
{
cin>>num;
if(num == -9999)
{
cout << "ABORTING.." << endl;
break;
}
Sum+=num;
}
cout << "Sum is:" << Sum << endl;
}