jeudi 5 mars 2015

How to use a catch try catch?


My professor has given us a try catch block that he wants us to use for all of our code from now on for now. After reading up on it I am still a little bit confused on how to implement it into my programs.


The code he wants us to implement into our programs.



try {
return 0;
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
keep_window_open();
return 2;
}


One of the programs I am working on.



#include "std_lib_facilities_4a.h"
bool yes()
{
string y;
cin >> y;
if (y[0] == 'y' || y[0] == 'Y')
{
return true;
}
else
return false;
}
int main()
{
enum {question1=1, question2=2, question3=4};
int result = 0;
cout << " Is what your thinking of red?\n";
if (yes() == true)
{
result += question1;
}
cout << " Is what your thinking of round?\n";
if (yes() == true)
{
result += question2;
}
cout << " Is what your thinking of an animal?\n";
if (yes() == true)
{
result += question3;
}
vector<string> answer = {"You are thinking of a blue square!.","You are thinking of a red square!","you are thing of a blue circle!","You are thinking of a red circle!","you are thinking of a blue animal!","you are thinking of a red animal!","you are thinking of a blue round animal!","You are thinking of a red turtle!"};
cout << answer[result];
return 0;
}



Aucun commentaire:

Enregistrer un commentaire