try, throw, catch exceptions

Hi all, ive been writing this same code for the last few days. I'm trying to utilize the try, throw, and catch exceptions. So my code compiles as I posted below. But I know for a fact I'm not using the exceptions correctly. I've only gone over exceptions the last two days, so I really still only understand a little bit of them. How would I get the exceptions to run my code but check for any unexpected errors?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <stdexcept> //Exception handling.
using namespace std;


int main()
{
	
	int bArray[16] = { 0 };
	int k = 0;

	int oArray[8] = { 0 };
	int i = 0;

	int hArray[8] = { 0 };
	int j = 0;

	int decimal = 0;
	int number;

	cout << "Enter 'B' to convert a decimal number to binary, 'O' for an octal conversion,and'H' for a hexadecimal conversion. "
		<< "Hit space to quit program." << endl;

	try{
		while ((number = cin.get()) != ' ')
		{
			
			cout << "\nEnter a decimal number to be converted (No more than 65,535): " << endl;
			
			switch (number)
			{
			case 'B':
			case 'b':
				cin >> decimal;
				while ((decimal != 0) & (k <= 15))
				{
					bArray[k] = decimal % 2;
					decimal = decimal / 2;
					k++;
				}

				cout << "Here is the binary conversion: ";

				for (k = 15; k >= 0; k--)
				{
					cout << bArray[k];
					if ((k % 4) == 0)
						cout << " ";
				}
				cout << endl;
				break;

			case 'O':
			case 'o':
				cin >> decimal;
				while ((decimal != 0) & (i <= 7))
				{
					oArray[i] = decimal % 8;
					decimal = decimal / 8;
					i++;
				}

				cout << "Here is the octal conversion: ";

				for (i = 7; i >= 0; i--)
				{
					cout << oArray[i];
					if ((i % 4) == 0)
						cout << " ";
				}
				cout << endl;
				break;

			case 'H':
			case 'h':
				cin >> decimal;

				for (int j = decimal; j > 0; j--){
					hArray[7] = hArray[7] + 1;
					if (hArray[7] > 15){
						for (int n = 6; n > -1; n--){
							hArray[n + 1] = 0;
							hArray[n] = hArray[n] + 1;
							if (hArray[n] < 16){
								n = -1;
							}
						}
					}
				}
				cout << "\n\nConverted to Hexidecimal is: ";
				for (int j = 0; j < 8; j++)
				{
					if (hArray[j] == 10){ cout << 'A'; }
					else if (hArray[j] == 11){ cout << 'B'; }
					else if (hArray[j] == 12){ cout << 'C'; }
					else if (hArray[j] == 13){ cout << 'D'; }
					else if (hArray[j] == 14){ cout << 'E'; }
					else if (hArray[j] == 15){ cout << 'F'; }
					else
					{
						cout << hArray[j];


					}
				}

				break;

			case '\n':
			case '\t':
			case ' ':
				break;

			default:
				cout << "Wrong decimal input." << "Enter a new number." << endl;
				break;
			}
		}
		throw 99;
	}
	catch (int x)
	{
		cout << "Error: " << x << endl;
	}

	cout << "Thank you." << endl;//end main
}
Just wanted to update my situation. I solved a bit of it on my own. However my code is "throwing" the wrong function I guess. Originally my exception handling is suppose to deal with the input for B, O, or H. If someone put in like K I'll display the error with the exception. Instead my exception and error display shows up when the user is entering a decimal after they've chosen what they want converted (B, H, or O). Here's my updated code:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

#include <iostream>
#include <stdexcept> //Exception handling.
using namespace std;


int main()
{
	
	int bArray[16] = { 0 };
	int k = 0;

	int oArray[8] = { 0 };
	int i = 0;

	int hArray[8] = { 0 };
	int j = 0;

	int decimal = 0;
	int number;

	cout << "Enter 'B' to convert a decimal number to binary, 'O' for an octal conversion,and'H' for a hexadecimal conversion. "
		<< "Hit space to quit program." << endl;

	try{
		while ((number = cin.get()) != ' ')
		{
			cout << "\nEnter a decimal number to be converted (No more than 65,535): " << endl;
			
			switch (number)
			{
			case 'B':
			case 'b':
				cin >> decimal;
				if ((decimal != 'B') && (decimal)){
					throw 99;
				}
				while ((decimal != 0) & (k <= 15))
				{
					bArray[k] = decimal % 2;
					decimal = decimal / 2;
					k++;
				}
				cout << "Here is the binary conversion: ";

				for (k = 15; k >= 0; k--)
				{
					cout << bArray[k];
					if ((k % 4) == 0)
						cout << " ";
				}
				cout << endl;
				break;

			case 'O':
			case 'o':
				cin >> decimal;
				if ((decimal != 'O') && (decimal != 'o')){
					throw 99;
				}
				while ((decimal != 0) & (i <= 7))
				{
					oArray[i] = decimal % 8;
					decimal = decimal / 8;
					i++;
				}

				cout << "Here is the octal conversion: ";

				for (i = 7; i >= 0; i--)
				{
					cout << oArray[i];
					if ((i % 4) == 0)
						cout << " ";
				}
				cout << endl;
				break;

			case 'H':
			case 'h':
				cin >> decimal;
				if ((decimal != 'H') && (decimal != 'h')){
					throw 99;
				}
				for (int j = decimal; j > 0; j--)
				{
					hArray[7] = hArray[7] + 1;

					if (hArray[7] > 15)
					{
						for (int n = 6; n > -1; n--)

						{
							hArray[n + 1] = 0;
							hArray[n] = hArray[n] + 1;

							if (hArray[n] < 16)
							{
								n = -1;
							}
						}
					}
				}
				cout << "\n\nConverted to Hexidecimal is: ";
				for (int j = 0; j < 8; j++)
				{
					if (hArray[j] == 10){ cout << 'A'; }
					else if (hArray[j] == 11){ cout << 'B'; }
					else if (hArray[j] == 12){ cout << 'C'; }
					else if (hArray[j] == 13){ cout << 'D'; }
					else if (hArray[j] == 14){ cout << 'E'; }
					else if (hArray[j] == 15){ cout << 'F'; }
					else
					{
						cout << hArray[j];


					}
				}

				break;

			case '\n':
			case '\t':
			case ' ':
				break;

			default:
				cout << "Wrong decimal input." << "Enter a new number." << endl;
				break;
			}
		}
		
	}
	catch (int x)
	{
		cerr << "Error: please run program again." << x << endl;
	}

	cout << "Thank you." << endl;//end main
}


I appreciate all the help I've been getting with this code.
You are trying to enter a character ('H') into an integer (decimal). You are also trying to compare an integer (number) with a character (' ', or 32).

I have a few recommendations:

1) replace decimal with the datatype of char

2) Change lines 38 and 61 to use a logical AND rather than binary AND

3) Remove your throw statements from within the cases and instead throw from inside the default case

4) Put in a check for if the decimal is larger than 65536, and if so, throw

5) Rather than throwing error codes (what does "99" mean?) throw meaningful data, such as std::runtime_error or std::range_error(see http://www.cplusplus.com/reference/stdexcept/ )

6) Have a catch block that catches arbritatry exceptions, and another to catch everything (just in case) (this isn't really necessary, but I like to know that my program crashed due to exception rather than something like segfault). Here is an example:
1
2
3
4
5
6
7
8
9
10
try {
    //... main function
}
// specialized catch blocks for error handling, and then:
catch (std::exception& e) {
    std::cerr << "Unhandled exception caught: " << e.what() << "\n";
}
catch (...) {
    std::cerr << "Unrecognized expection caught.\n";
}
Thanks for the reply. I've changed everything you've recommended. I can't believe I've been using the datatype int instead of char for my switch statements. Anyway I really don't know how use the throw in the default case. So far I have:
1
2
3
default:
  throw invalid_argument ("Wrong input");
  break;
First, looking over my post before, I seem to have made a mistake (I do that a lot). I meant to say for point one, to make "number" a char (get the type of the number), not make "decimal" a char.

What you have there is pretty much what you want. However, rather than just saying "Wrong Input", which we have gathered from it being an "invalid argument"-type error, you could give details on the error (such as saying what character was recieved and what were required, like this:
1
2
3
default:
    throw std::invalid_argument ('"' + number + "\" recieved; B O or H expected");
    // break not required due to the throw terminating program run 


Then you could have a catch block to catch instances of std::invalid_argument, and display an error saying something like:
1
2
3
catch (const std::invalid_argument& e) {
    std::cerr << "Error: Invalid Argument: " << e.what() << "\n";
}


In this case, it would say if I input L:
Error: Invalid Argument: "L" recieved, B O or H expected


However, it seems that you get the idea.
Topic archived. No new replies allowed.