【问题标题】:How to properly have a function throw a std::out_of_range exception如何正确地让函数抛出 std::out_of_range 异常
【发布时间】:2020-10-01 14:37:30
【问题描述】:
else
{
    // figure out how to fo out of range excetpion.
    throw std::out_of_range("An exception occurred: Grade must be between 0.000000 and 100.000000");
    //throw std::out_of_range("Grade points must be between 0.000000 and 100.000000.");
}   

我得到错误:

Percentage grade: 10000
terminate called after throwing an instance of 'std::out_of_range'
  what():  An exception occurred: Grade must be between 0.000000 and 100.000000"

如何删除这个?:

terminate called after throwing an instance of'std::out_of_range'
  what():"

【问题讨论】:

  • FWIW,你真的不应该使用异常。错误的输入并没有什么特别之处,你应该只在某些事情真正异常时才使用异常。对于错误的输入,您应该使用错误代码或只是在输入正确数据之前不会停止的循环。
  • catch 异常。

标签: c++ throw outofrangeexception


【解决方案1】:

你可以删除这个:

terminate called after throwing an instance of'std::out_of_range'
  what():"

使用类似的代码,如下所述:

int x = 10;
int y = 0;
int z = 0;

try {
    if (y == 0) throw ("Something wrong going on!"); // throwing string as error

    z = x / y;

    std::cout << z << std::endl;

} catch (const char *m) {
    std::cerr << m << std::endl; // printing error
}

你会得到类似的东西:

Something wrong going on!

消息。当你使用像std::out_of_range 这样的类时,它会显示在程序执行期间它实际从哪里抛出的信息。你可以通过不使用函数来抑制它,直接在 throw 语句中使用字符串,不要忘记捕获字符串以获取异常。

【讨论】:

  • "你可以通过不使用函数来抑制它并直接在 throw 语句中使用字符串" - 这有点误导。即使你抛出一个字符串,你仍然必须抓住它,否则应用程序将被终止。 std::out_of_range 也是一个类,而不是一个函数。
【解决方案2】:

当你扔的时候,你需要接住......

#include <iostream>
#include <exception>
using namespace std;

struct OORException : public exception {
    const char* what () const throw () {
        return "Exception out of range: Grade must be between 0.000000 and 100.000000";
    }
};

int main () {

    // Use an application defined exception
    try
    {
        // value out of range exception.
        throw OORException();
        // throw std::out_of_range("An exception occurred: Grade must be between 0.000000 and 100.000000");
        //throw std::out_of_range("Grade points must be between 0.000000 and 100.000000.");
    }
    catch(OORException& e) {
        std::cout << "OORException caught" << std::endl;
        std::cout << e.what() << std::endl;
    }

    // use a predefined std::exception
    try
    {
        // value out of range exception.
        throw std::out_of_range("Exception occurred: Grade must be between 0.000000 and 100.000000");
    }
    catch (std::out_of_range& e)
    {
        std::cout << "Exception Out Of Range " << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
        std::cout << "An exception occurred. Exception " << e.what() << std::endl;
    }
    return 0;
}

在这种情况下你是否应该扔/接是另一个问题。

【讨论】:

    【解决方案3】:

    throw'ing std::out_of_range 异常本身不是问题。真正的问题是std::terminate() 会在线程抛出异常并且没有捕获它时自动终止调用进程。

    因此,您需要在 try 块和 catch 异常中调用您的抛出函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      相关资源
      最近更新 更多