gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:56:41 25 4
gpt4 key购买 nike

我最近开始使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com