How do I more properly integrate an exception into my code, and future code? (C++) -
i've begun programming c++ competitions in school, state, etc. i've not had lot of practice , i'm still fresh c++, , i've run issue programs if character entered instead of numerical value, loop run continuously without asking input. program wrote basic chemical conversion bars:
#include "stdafx.h" using namespace std; class exception : public exception { public: exception(string m = "exception!") : msg(m){} ~exception() throw() {} const char* what() const throw() { return msg.c_str(); } private: string msg; }; int _tmain(int argc, _tchar* argv[]) { // 'n' stands numerator , 'd' stands denominator int choice; float n1; float n2; float n3; float n4; float d1; float d2; float d3; float answer; while (true) { cout << "select how many conversion bars have: " << endl; cin >> choice; if (choice == 1) {cout << "what starting number?" << endl; cin >> n1; cout << "what first bar (enter numerator denominator): " << endl; cin >> n2; cin >> d1; answer = n1 * n2 / d1; cout << "answer: " << answer << endl << endl;} else if (choice == 2) { cout << "what starting number?" << endl; cin >> n1; cout << "what first bar (enter numerator denominator): " << endl; cin >> n2; cin >> d1; cout << "what second bar (enter numerator denominator): " << endl; cin >> n3; cin >> d2; answer = (n1 * n2 * n3) / (d1 * d2); cout << "answer: " << answer << endl << endl; } else if (choice == 3) { cout << "what starting number?" << endl; cin >> n1; cout << "what first bar (enter numerator denominator): " << endl; cin >> n2; cin >> d1; cout << "what second bar (enter numerator denominator): " << endl; cin >> n3; cin >> d2; cout << "what third bar (enter numerator denominator): " << endl; cin >> n4; cin >> d3; answer = (n1 * n2 * n3 * n4) / (d1 * d2 * d3); cout << "answer: " << answer << endl << endl; } else if ((choice /= 1) || (choice /= 2) || (choice /= 3)) {cout << "that not valid option." << endl << endl;} try{ throw exception();} catch (exception& e) { cout << e.what() << endl; break; } } return 0; }
and headers:
#pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <iostream> #include <cstdint> #include <cstdbool> #include <string>
so how better integrate exceptions code , future code?
three basic rules exceptions (of course more personal flavor maybe find useful):
do not use exceptions control flow - use control flow statements (if, while)
//good: while(isvalidinput) { // check validness // e.g. if (atoi(input) != 1) ... }
do not hide caught exceptions own exception types (you know wrong - hide in upper layers)
//bad: ... catch(std::ios_base::failure& ex) { throw mydataformatexception("format error"); }
use exceptions low level possible (you wasting lots of cpu power , can risk not realizing new errors in program because caught in main block)
//bad: int main() { try { /* */ } catch(...) {} }; // good: try { std::ifstream inputfilestream("~/file.txt"); } catch(std::ios_base::failure&) { ... }
Comments
Post a Comment