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
#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
"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.
>> 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah.. makes sense now.. thanks very much..
ASKER
***should be "the second time getValue is called***