I want to write an StrExcept exception class that inherits from the base exception class out_of_range by defining its own constructor and a print() method outputting a custom error message to the user.
For now this is my code:
class StrExcept: public out_of_range
{
string message;
public:
StrExcept():out_of_range("The value entered is out of range"),message("The value entered is out of range"){};
void print(){
cout << message;
}
};
Inside main method:
try
{
string str="My name";
// the exception will be fired
str.substr(11,2);
}
// i want to catch it using this custom class
catch (StrExcept &outOfRange )
{
// i want to print the error message using print method
outOfRange.print();
}
But why is not working ??? and the program crash ???
Aucun commentaire:
Enregistrer un commentaire