### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````#include #include //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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142`` `````` #include #include //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:
 ``12345678910`` ``````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:
 ``123`` ``````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:
 ``123`` ``````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:
 ``123`` ``````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.