Jump to content

A simple exception problem in c++


Recommended Posts

#include<iostream>
#include<string>

using namespace::std;

class NonIntegerException : public runtime_error
{
    private:
	 string nonIntegerData;
    public:
	 NonIntegerException(string);
	 string getNonIntegerData();
};

NonIntegerException::NonIntegerException(string s) : runtime_error("Non-integer data")
{
nonIntegerData = s;
}

string NonIntegerException::getNonIntegerData()
{
return nonIntegerData;
}

int getInt(string prompt)
{
int integer;
string badData;

cout << prompt;

if(!(cin >> integer))
{
	cin.clear();
	cin >> badData;
	[b]NonIntegerException e(badData);
	throw e;[/b]
}

return integer;
}

int main()
{
int age;

try
{
     age = getInt("Enter your age ");
}

catch(NonIntegerException)
{
	cout <<[b] e.what()[/b] << endl;
	cout << "The entered value was " <<[b] e.getNonIntegerData();[/b]
	cout << "Age is being set to 0" << endl;

	age = 0;
}

return 0;
}

 

The problem areas are bolded and also here are the errors that are popping up:

 

1>.\Customexception.cpp(59) : error C2065: 'e' : undeclared identifier

1>.\Customexception.cpp(59) : error C2228: left of '.what' must have class/struct/union

1> type is ''unknown-type''

1>.\Customexception.cpp(60) : error C2228: left of '.getNonIntegerData' must have class/struct/union

1> type is ''unknown-type''

1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Customexception\Customexception\Debug\BuildLog.htm"

1>Customexception - 3 error(s), 0 warning(s)

 

The thing is here, I know that e is defined.

 

Nevermind, Figured out that the problem was that I left out the "e" in the catch parentheses.

 

The small stuff always throws you for a loop......

Quote - Revenge is such a nasty thing that only breeds more vengeful souls, but in some situations revenge does not even need to be sought out, but only bided.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.