Exception Handling, How? Try/Catch throw...

This is a discussion on Exception Handling, How? Try/Catch throw... within the C++ Programming forums, part of the General Programming Boards category; I have a menu that a user must enter a number to navigate. When the user enters anything else but ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Exception Handling, How? Try/Catch throw...

    I have a menu that a user must enter a number to navigate. When the user enters anything else but a integer the program goes into a infinte loop. So I am trying to do some exception handling work. Hear is what I have....
    Code:
    void Controller::CommandControl(int input)
    {
    	try{
    		if (input == 1)
    		{
    			menu.Info(); //Displays info about program
    		}
    		else if (input == 2)
    		{
    			CdInfoMenu();
    		}
    		else if (input == 3)
    		{
    			cout << "Goodbye" << endl; //End Program
    		}
    		else if (input != 1,2,3)
    		{
    			throw input;
    		}
    		else
    		{
    			cout << input << ":  is not a valid command" << endl;	
    		}
    	}
    	catch (char input)
    	{
    		cout << input << ": is not a vaild command" << endl;
    	}
    }
    Is it possible to through a data type. I want to be able to through any input that is not an int and catch in a catch block. How do you through a data type i.e char or string.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    This is nonsense - "(input != 1,2,3)"

    Why are you throwing an int and trying to catch a char?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by Fordy
    This is nonsense - "(input != 1,2,3)"

    Why are you throwing an int and trying to catch a char?

    Because I don't know what I am doing

    Should I be doing something more like
    Code:
    else if (input != int)
    		{
    			throw input;
    		}
    and then have a catch block for every other variable type. Like I said I am trying to protect the user from inputing anything else then an int.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You are passing an int to the function, therefore, there is no need to use a try/catch combination here. Someplace before this function, where the user actually inputs information I would do data/input validation probably by testing the stream or parsing a string, but I wouldn't use try/catch personally.
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    This is were it is coming from....
    Code:
    void GetInput()
    {
    	Controller controlInput;
    	Menu menu;
    	int input;
    
    	do{
    		menu.MainMenu();
    		cout << endl << "Enter Option: " ;
    		cin >> input;	
    		cout << endl << endl ;
    
    	controlInput.CommandControl(input);
    	}
    	while (input != 3);
    
    }
    And your saying a try/catch block is a bad idea even in this function. Can you suggest what I should use besides a try/catch block? I am not sure if I know what you mean by validation.
    Last edited by chadsxe; 07-19-2005 at 08:35 AM.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I usually declare a blank class then throw that.

    Code:
    class InputError {};
     
    try 
    {
    	//etc
    	throw InputError;
    } 
    catch (InputError)
    {
    	// error message etc
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by ahluka
    I usually declare a blank class then throw that.

    Code:
    class InputError {};
     
    try 
    {
    	//etc
    	throw InputError;
    } 
    catch (InputError)
    {
    	// error message etc
    }
    Wow....

    Can you please expalin this. I have only been at this for a couple months now and the more information I can gather the better.

    Thanks

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yes, this is where I'd test the input. You could do it here (if you test the stream) or send a string to another function to validate it.


    Code:
    //example of stream validation. It isn't the most robust (the ignore condition could be more sophisticated), but will work under most conditions. 
     
    bool inputIsInvalid;
    do
    { 
      inputIsInvalid = true;
      while(inputIsInvalid)
      {
    	menu.MainMenu();
     
    	cout << endl << "Enter Option: " ;
    		 cin >> input;
       if(cin.fail)
       { 
    	 cin.clear();
    	 cin.ignore(3000);
    	 cout << "invalid input. try again" << endl;
       }
       else
    	 inputIsInvalid = false
     }
      cout << endl << endl ;
     
      controlInput.CommandControl(input);
    }while (input != 3);
    Code:
    //example of using a string for data/input validation
    bool inputIsInvalid;
    char input[80];
    int length;
    int i;
    do
    { 
    inputIsInvalid = true;
    while(inputIsInvalid)
    {
    	 menu.MainMenu();
     
    	 cout << endl << "Enter Option: " ;
    	 cin >> input;
    	 length = strlen(input);
    	 if(length > 1)
    	 cout << "input is invalid" << endl;
    	 else if(!isdigit(input[0]))
    	 cout << "input is invalid" << endl;
    	 else
    	 {
    		i = atoi(input);
    		inputIsInvalid = false;
    	 }
    	 cout << endl << endl ;
     
    	 controlInput.CommandControl(i);
    }while (i != 3);
    In either case you have gauranteed that CommandControl will be passed an int. Then, in CommandControl you can use your switch statement to determine what to do. You might want to vailidate the size of the int before sending it to CommandControl since an int with value greater than 3 is invalid for the purposes of your program and you could ask for user to repeat input all in the same place.
    You're only born perfect.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    In the first example you have
    Code:
    if(cin.fail)
    What does the "fail" stand for?

    In your second example can you please break down
    Code:
     else if(!isdigit(input[0]))
    and
    Code:
    i = atoi(input);
    for me.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    it should have been cin.fail(), sorry. fail() is a method of the istream class that detects whether valid input is found in the input buffer. If you try to enter a char into an int, that is invalid, so the fail bit of the istream state variable is flipped and attempts at input into the variable cease, and you can't use the stream any longer until it is cleared with the clear() method. fail() and good() look at the fail bit of the state variable and return true or false as appropriate.


    isdigit() is a member of the cstring (or string.h if you must) library (or header file). If the char sent to isdigit() is a digit, it will return true.

    atoi() converts a C style string to an int. I believe it is also in the cstring library, though it may be elsewhere. You can look it up for sure.
    You're only born perfect.

  11. #11
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by chadsxe
    Wow....

    Can you please expalin this. I have only been at this for a couple months now and the more information I can gather the better.

    Thanks
    Well to be completely honest I don't know. I just know you can do it; there may be little difference to how you would otherwise go about it.

    All I can advise is you look for some good C++ tutorials and jump to the error handling section - I'm doing the same after I've written this so I'll post up some links if I find any good 'uns
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by elad
    it should have been cin.fail(), sorry. .
    No reason to apologize. I can't thank you enough for the help along with everyone else. I am going to go and try to implement some of this into my program. I am sure I will be back with more question.

    Thanks

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I think i have it all set up correct but now I am throwing and error in my main funciton. I used the second method you showed me because I actually understand whats going on. Hear is how it looks
    Code:
    void GetInput()
    {
    	Controller controlInput;
    	Menu menu;
    	bool inputIsInvalid;
    	char input[80];
    	int length;
    	int i;
    
    do
    	{ 
    		inputIsInvalid = true;
    				
    		while(inputIsInvalid)
    			{
    				menu.MainMenu();
     
    				cout << endl << "Enter Option: " ;
    				cin >> input;
    				length = strlen(input);
    				if(length > 1)
    				{
    					cout << "input is invalid" << endl;
    				}
    				else if(!isdigit(input[0]))
    				{
    					cout << "input is invalid" << endl;
    				}
    				else
    				{
    					i = atoi(input);
    					inputIsInvalid = false;
    				}
    
    		cout << endl << endl ;
    		controlInput.CommandControl(i);
    	}
    	while (i != 3);
    }
    void main()
    { //error C2062 is pointing to this line
    	Menu::Info();
    	GetInput();	
    }
    This is produceing error C2062: type 'void' unexpected were I marked in my code.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void main()
    Now all you have to do is go read the FAQ so you can see why this is wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by quzah
    Code:
    void main()
    Now all you have to do is go read the FAQ so you can see why this is wrong.


    Quzah.
    Well I just checked over the FAQ and updated my code as was recommended.
    Code:
    int main ( int argc, char *argv[] )
    {
    	Menu::Info();
    	GetInput();	
    }
    I am now getting this error

    error C2062: type 'int' unexpected

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 06:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  3. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  4. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21