Avatar of gil151
gil151
 asked on

Input exceptions

This program is just being used to test exceptions..It should throw exceptions until the value falls within the given range (here it is between 1 and 11)..  Doesnt have any real functionality, other than that..    It catches and handles the exception the first time through (using an input value of 11, which is "out of range" for this situation.. anyway if i input 12 the second time subRange is called (out of the catch statement), i get an error message "unhandled exception: int @ some memory address", instead of the generic "E" being thrown and being caught again in main..  i have a suspicion that it has something to do with the stack..  if anyone could explain the "why" behind this, i would appreciate it..

#include <iostream>


using namespace std;

class subRange
{

public:
      subRange(int, int);
      subRange();
      int getValue( );

private:
      int lower;
      int upper;

};

subRange::subRange(int low, int high)
{
      lower = low;
      upper = high;
}

int subRange::getValue()
{      
      
                  int v;
                  int E = 1;
                  cout << "Enter value [ " << lower << ", " << upper << " ]: ";
                  cin >> v;
                  if(v < lower || v > upper)
                  {
                        throw E;  //unhandled error here after second call..
                  }
                  return v;
}



void main()
{
      subRange x(1, 10);
      
      try
      {
            x.getValue();
      }
      
                catch(int )
      {
            cout << "\nValue out of Range! \n";
            
            cout << x.getValue() << endl;
      }
      
}


thanks... scf
C++

Avatar of undefined
Last Comment
gil151

8/22/2022 - Mon
gil151

ASKER
the second time subRange is called (out of the catch statement

***should be "the second time getValue is called***
AlexFM

"Unhandled exception" - this is exactly what happens in your program.

>> anyway if i input 12 the second time subRange is called (out of the catch statement), i get an error message "unhandled exception.

If your program doesn't catch exception, OS does this. Exception message is the way to handle unhandled exception: show error message and terminate the program. If some function can throw exception, catch it. And don't call the same function again inside of catch block: this doesn't make sence.
SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gil151

ASKER
Ah..  makes sense now..  thanks very much..
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23