Try Catch

May 21, 2012 at 3:06pm
I have been reading a little about try catch blocks. Could someone show me an example of a try catch that could be used with something like this.

1
2
int choice;
cin >> choice;


Now if you were to input a letter the program either crashes or in my case because I have everything in a bit loop, it goes in to a infinite loop because I check the value of choice later.

So how can the try catch be used to make sure an integer is entered.
May 21, 2012 at 3:33pm
Non-integer input is not an exceptional condition, you should check the status of the input stream with an if (or use it as a part of the loop statement):

1
2
3
4
5
6
7
int choice;
cin >> choice;
if(!cin)
{
    cout << "What was that?\n";
    break;
}


But if you want to know how it could be done with exceptions,

1
2
3
4
5
6
7
8
9
10
cin.exceptions(ios_base::failbit); // throw on rejected input
try {
// some code
int choice;
cin >> choice;
// some more code
} catch(const ios_base::failure& e) {
    cout << "What was that?\n";
    break;
}
Last edited on May 21, 2012 at 3:34pm
May 21, 2012 at 3:39pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;
main()
{


    try
    {
     int choice;
     int choice2;
     cout<<"Give first choice:";
     cin>>choice;
     cout<<"Give second choice:";
     cin>>choice2;

          if(choice>choice2)
          {
              throw 60;
          }
    }

   catch(int x)
   {
       cout<<"\nchoice cant be bigger than choice1."<<"Error:"<<x<<endl;
   }
}



I HOPE IT WILL HELP YOU.
answer here to know if you want more or if it is ok and it helped you.
Last edited on May 21, 2012 at 3:39pm
May 22, 2012 at 3:45am
I got Cubbi's answer to work but even though im catching the exception, the program crashes when a letter is entered but with a fancy message now. Also I could not get your first example to work, only with exceptions did i manage to get it to work some what.
Last edited on May 22, 2012 at 4:56am
May 22, 2012 at 8:12am
His first example didn't handle the error.

The program doesn't crash with the exception. You simply don't do anything to recover and and the program ends.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <limits>
#include <iostream>

using namespace std ;

int get_int_normal()
{
	int number ;
	while ( cout << "Enter a number: " && !(cin >> number) )
	{
		cout << "That wasn't a number!\n\n" ;
		cin.clear() ;  // clear error state

		// ignore non-numeric input:
		cin.ignore(std::numeric_limits<streamsize>::max(), '\n') ;
	}
	return number ;
}

int get_int_exception()
{
	// save current exception settings
	auto prevExceptionMask = cin.exceptions() ;
	cin.exceptions(ios::failbit) ;
	
	for ( ; ; )
	{
		int number ;

		cout << "Enter a number: " ;

		try {
			cin >> number ;
		}

		catch (const ios::failure &)
		{
			cout << "That wasn't a number!\n\n" ;
			cin.clear() ;
			cin.ignore(std::numeric_limits<streamsize>::max(), '\n') ;
			continue ;
		}

		// restore previous exception settings
		cin.exceptions(prevExceptionMask) ;
		return number ;
	}
}


int main()
{
	int number = get_int_normal() ;
	
	cout << "You entered " << number << '\n' ;

	number = get_int_exception() ;

	cout << "You entered " << number << '\n' ;
}
May 22, 2012 at 3:08pm
numeric_limits is not a part of std apparently.
May 22, 2012 at 3:13pm
May 22, 2012 at 3:14pm
Never mind i forgot to include limits, is either way better?
Topic archived. No new replies allowed.