1

In the following program:

int main(){

    std::cout<<"enter numbers to be divide"<<std::endl;
    int a,b,c;

    while(true){
        try{
            if(!(std::cin>>a>>b)){
                throw std::invalid_argument("please enter proper interger");
            }

            if(b==0){
                throw std::runtime_error("please enter enter nonzero divisor");
            }
            c=a/b;
            std::cout<<"quotient = "<<c<<std::endl;
        }
        catch(std::invalid_argument e){
            std::cout<<e.what()<<"\ntry again?enter y/n";
            char c;
            std::cin>>c;
            if(c=='n'||c=='N') break;
        }
        catch(std::runtime_error e){
            std::cout<<e.what()<<"\ntry again?enter y/n";
            char c;
            std::cin>>c;
            if(c=='n'||c=='N') break;
        }
    }
    return 0;
}

I am using two kinds of exception.Program is working perfectly when it throws "runtime_error" exception but goes into infinite loop when encounter "invalid_argument" exception. Actually there is problem in "cin>>c" statement in catch-block but can not figure out, why this is happening.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
White Dwarf
  • 189
  • 1
  • 3
  • 11
  • program works perfectly when i remove whole input scenario from catch-block of "invalid_argument" and put only "break" statement. – White Dwarf Jul 08 '11 at 13:10
  • See this answer: http://stackoverflow.com/questions/2407771/c-character-to-int/2407991#2407991. – kennytm Jul 08 '11 at 13:14
  • Thanks Kenny, I use two statements to flush out cin stream. " std::cin.clear(); std::cin.ignore(80, '\n');" since "std::numeric_limits::max()" giving error. program is working perfectly now – White Dwarf Jul 08 '11 at 13:43

2 Answers2

3

When std::cin>>a>>b encounters a non-numeric character, two relevant things happen:

  • the offending character is not consumed;
  • the fail bit of std::cin is set.

The latter prevents all further reads from std::cin from succeeding. This includes those inside your invalid_argument catch block and those inside subsequent iterations of the loop.

To fix this, you need to clear the state of std::cin and to consume the offending character. This is explained very well in the following answer pointed out by KennyTM: C++ character to int.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You can play with exception masks, you might find a preferable way to handle errors.

spraff
  • 32,570
  • 22
  • 121
  • 229