c++ - 我如何更恰本地将异常集成到我的代码和 future 的代码中? (C++)

标签 c++ exception

我最近开始使用 C++ 为我的学校、州等举办的比赛进行编程。我没有太多的练习,我对 C++ 还是很陌生,我遇到了这个问题如果输入字符而不是数值,循环将连续运行而无需输入。最近我写了这个程序来做基本的化学转化棒:

#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 for numerator and  'd' stands for 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 you have: " << endl;
        cin >> choice;
            if (choice == 1)
                {cout << "What is the starting number?" << endl;
                cin >> n1;
                cout << "What is the first bar (Enter Numerator then Denominator): " << endl;
                cin >> n2;
                cin >> d1;
                answer = n1 * n2 / d1;
                cout << "Answer: " << answer << endl << endl;}
            else if (choice == 2)
            {
                cout << "What is the starting number?" << endl;
                cin >> n1;
                cout << "What is the first bar (Enter Numerator then Denominator): " << endl;
                cin >> n2;
                cin >> d1;
                cout << "What is the second bar (Enter Numerator then Denominator): " << endl;
                cin >> n3;
                cin >> d2;
                answer = (n1 * n2 * n3) / (d1 * d2);
                cout << "Answer: " << answer << endl << endl;
            }
            else if (choice == 3)
            {
                cout << "What is the starting number?" << endl;
                cin >> n1;
                cout << "What is the first bar (Enter Numerator then Denominator): " << endl;
                cin >> n2;
                cin >> d1;
                cout << "What is the second bar (Enter Numerator then Denominator): " << endl;
                cin >> n3;
                cin >> d2;
                cout << "What is the third bar (Enter Numerator then 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 is not a valid option." << endl << endl;}
            try{ throw Exception();}
                catch (exception& e)                    
            {
                cout << e.what() << endl;
                break;
            }
        }
return 0;
}

还有标题:

#pragma once

#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <cstdint>
#include <cstdbool>
#include <string>

那么我怎样才能更好地将异常集成到这段代码和任何 future 的代码中呢?

最佳答案

异常的三个基本规则(当然这更像是个人喜好,但也许您会发现它很有用):

  1. 不要将异常用于控制流 - 使用控制流语句(if、while)

    //good:
    while(isValidInput)
    {
        // check your validness 
        // e.g. if (atoi(input) != 1) ...
    }
    
  2. 不要用自己的异常类型隐藏已经捕获的异常(您已经完全知道哪里出了问题 - 您只会在上层隐藏它)

    //bad:
    ...
    catch(std::ios_base::failure& ex)
    {
        throw myDataFormatException("Format error");
    }
    
  3. 尽可能低级别地使用异常(您正在浪费大量的 CPU 能力,并且您可能会冒着没有意识到程序中新错误的风险,因为一切都只是在主 block 中被捕获)

    //bad:
    int main() { try { /* everything */ } catch(...) {} };
    // good:
    try
    {
        std::ifstream inputFileStream("~/File.txt");
    }
    catch(std::ios_base::failure&) { ... }
    

关于c++ - 我如何更恰本地将异常集成到我的代码和 future 的代码中? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060392/

相关文章:

c++ - 为什么在 main 中未捕获异常时不调用析构函数?

java - Apache 米娜异常

c++ - C++如何继承Template类

c++ - 如何在 C++ 中解析数组以查找重复项

c++ - 从 C++ 中静态对象的 C'tor 抛出/捕获异常

c# - 如何处理大型代码库中的异常

exception - 戈朗 : Defensive interfaces for nil types

c++ - 需要帮助理解 "rand() % 67 < 10"

c++ - 如何 self 复制一个 vector ?

c++ - 在 Eclipse 中使用 GDB 时出错 : "Command ' -list-features' is timed out"