C++ 抛出运行时错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26171631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:32:26  来源:igfitidea点击:

throwing a run time error

c++error-handling

提问by stanna23

I'm new to programming and i started Programming: Principles and Practice Using C++. In one of the chapters it talks about errors and how to handle them.

我是编程新手,我开始学习编程:使用 C++ 的原则和实践。在其中一章中,它讨论了错误以及如何处理它们。

The snippet of code here is what I'm trying to implement. In the book it states that error() will terminate the program with a system error message plus the string we passed as an argument.

这里的代码片段是我想要实现的。在书中它指出 error() 将终止程序,并显示系统错误消息和我们作为参数传递的字符串。

#include <iostream>
#include <string>

using namespace std;

int area (int length, int width)
{
    return length * width;
}

int framed_area (int x, int y)
{
    return area(x-2, y-2);
}

inline void error(const string& s)
{
    throw runtime_error(s);
}


int main()
{
    int x = -1;
    int y = 2;
    int z = 4;

    if(x<=0) error("non-positive x");
    if(y<=0) error("non-positive y");

    int area1 = area(x,y);
    int area2 = framed_area(1,z);
    int area3 = framed_area(y,z);

    double ratio = double(area1)/area3;

    system("PAUSE");
    return EXIT_SUCCESS;
}

The message i get is "Unhandled exception at 0x7699c41f in test project.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0038fc18.."

我收到的消息是“测试 project.exe 中 0x7699c41f 处的未处理异常:Microsoft C++ 异常:内存位置 0x0038fc18 处的 std::runtime_error ..”

So my question is, what am I doing wrong that the message I pass to error() isn't passed?

所以我的问题是,我传递给 error() 的消息没有通过,我做错了什么?

回答by Gillespie

As I mentioned in my comment, you have to "catch" errors that you "throw" in order for your program not to instantly terminate. You can "catch" thrown exceptions with a try-catch block, like this:

正如我在评论中提到的,您必须“捕获”您“抛出”的错误,以便您的程序不会立即终止。您可以使用 try-catch 块“捕获”抛出的异常,如下所示:

#include <iostream>
#include <string>

using namespace std;

int area (int length, int width)
{
    return length * width;
}

int framed_area (int x, int y)
{
    return area(x-2, y-2);
}

inline void error(const string& s)
{
    throw runtime_error(s);
}


int main()
{
    int x = -1;
    int y = 2;
    int z = 4;

    try
    {
        if(x<=0) error("non-positive x");
        if(y<=0) error("non-positive y");

        int area1 = area(x,y);
        int area2 = framed_area(1,z);
        int area3 = framed_area(y,z);

        double ratio = double(area1)/area3;
     }
     catch (runtime_error e)
     {
         cout << "Runtime error: " << e.what();
     }

    system("PAUSE");
    return EXIT_SUCCESS;
}

回答by Carl

First, I don't know how your program is compiling, you need to include stdexcept

首先,我不知道你的程序是如何编译的,你需要包含 stdexcept

To answer, your program is acting exactly as it should. You may have missed something in the reading, but the unfortunate thing is the error message you get from Microsoft. Here is the output I get on OSX:

要回答,您的程序完全按照它应有的方式运行。您可能在阅读中遗漏了一些内容,但不幸的是您从 Microsoft 收到的错误消息。这是我在 OSX 上得到的输出:

terminate called after throwing an instance of 'std::runtime_error'
  what():  non-positive x
Abort trap: 6

OSX gives me the contents of what(), so at least I know that it is my exception that has terminated the program.

OSX 给了我 的内容what(),所以至少我知道是我的异常终止了程序。

I'm assuming you are using Visual Studio, and I don't really know how to use it. Maybe if you compile the program in debug mode it will give more output as to what actually happened that an exception was thrown.

我假设您使用的是 Visual Studio,但我真的不知道如何使用它。也许如果你在调试模式下编译程序,它会给出更多关于抛出异常实际发生了什么的输出。

Anyway, this probably isn't the way you want the program to end, you should put the code that might throw an exception within tryblocks and then catchit:

无论如何,这可能不是您希望程序结束的方式,您应该将可能引发异常的代码放在try块中,然后catch

 int main()
{
    try
    {
      int x = -1;
      int y = 2;
      int z = 4;

      if(x<=0) error("non-positive x");
      if(y<=0) error("non-positive y");

      int area1 = area(x,y);
      int area2 = framed_area(1,z);
      int area3 = framed_area(y,z);

      double ratio = double(area1)/area3;
    }
    catch( const std::runtime_error& error )
    {
      std::cout << error.what() << '\n';
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}