Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何正确地让函数抛出std::out_of_range异常_C++_Throw_Outofrangeexception - Fatal编程技术网

C++ 如何正确地让函数抛出std::out_of_range异常

C++ 如何正确地让函数抛出std::out_of_range异常,c++,throw,outofrangeexception,C++,Throw,Outofrangeexception,我得到一个错误: 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.0000

我得到一个错误:

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():"
通过使用类似代码,如下所述:

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

信息。当您使用像
std::out_of_range
这样的类时,它将显示有关在程序执行期间实际从何处抛出的信息。您可以通过不使用函数和直接在throw语句中使用字符串来抑制它,并且不要忘记捕获异常字符串。

当您抛出时,您需要捕获

Something wrong going on!
#包括
#包括
使用名称空间std;
结构OORException:公共异常{
常量字符*什么()常量抛出(){
return“异常超出范围:等级必须介于0.000000和100.000000之间”;
}
};
int main(){
//使用应用程序定义的异常
尝试
{
//值超出范围异常。
抛出OORException();
//抛出标准::超出范围(“发生异常:等级必须介于0.000000和100.000000之间”);
//抛出标准::超出范围(“分数必须介于0.000000和100.000000之间”);
}
捕获(OORException&e){

std::cout
抛出
'ing
std::out\u of_range
异常本身并不是问题。真正的问题是,如果线程抛出异常且未捕获它,会自动调用以终止调用进程


因此,您需要在
try
块内调用抛出函数并
catch
异常。

FWIW,您真的不应该使用异常。错误输入没有什么异常,只有在真正异常的情况下才应该使用异常。对于错误输入,您应该使用错误代码或循环在输入正确的数据之前,该操作不会停止。
catch
异常。“您可以通过不使用函数而直接在throw语句中使用字符串来抑制它”-这有点误导。即使你抛出一个字符串,你仍然必须捕获它,否则应用程序将被终止。而且
std::out\u of_range
是一个类,而不是一个函数。
Something wrong going on!
#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;
}