Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++编程原理和实践。在其中一章中,它讨论了错误以及如何处理错误_C++_Error Handling - Fatal编程技术网

抛出运行时错误 我对编程很陌生,我开始使用C++编程原理和实践。在其中一章中,它讨论了错误以及如何处理错误

抛出运行时错误 我对编程很陌生,我开始使用C++编程原理和实践。在其中一章中,它讨论了错误以及如何处理错误,c++,error-handling,C++,Error Handling,这里的代码片段就是我试图实现的。在书中,它声明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-

这里的代码片段就是我试图实现的。在书中,它声明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;
}
#包括
#包括
使用名称空间std;
整数区域(整数长度、整数宽度)
{
返回长度*宽度;
}
内部框架面积(内部x,内部y)
{
返回区(x-2,y-2);
}
内联无效错误(常量字符串(&s)
{
抛出运行时错误;
}
int main()
{
int x=-1;
int y=2;
int z=4;

如果(x如我在评论中所述,您必须“捕获”您“抛出”的错误,以便您的程序不会立即终止。您可以使用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;
}
#包括
#包括
使用名称空间std;
整数区域(整数长度、整数宽度)
{
返回长度*宽度;
}
内部框架面积(内部x,内部y)
{
返回区(x-2,y-2);
}
内联无效错误(常量字符串(&s)
{
抛出运行时错误;
}
int main()
{
int x=-1;
int y=2;
int z=4;
尝试
{

如果(x首先,我不知道您的程序是如何编译的,您需要包括
stdexcept

为了回答这个问题,您的程序正按其应有的方式运行。您可能在阅读中遗漏了一些内容,但不幸的是,您从Microsoft收到了错误消息。以下是我在OSX上得到的输出:

terminate called after throwing an instance of 'std::runtime_error'
  what():  non-positive x
Abort trap: 6
OSX给了我
what()
的内容,所以至少我知道是我的异常终止了程序

我假设您使用的是Visual Studio,但我真的不知道如何使用它。也许如果您在调试模式下编译程序,它将提供更多关于抛出异常时实际发生的情况的输出

无论如何,这可能不是您希望程序结束的方式,您应该将可能引发异常的代码放在
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;
}
intmain()
{
尝试
{
int x=-1;
int y=2;
int z=4;

如果(x)是其中一章,它讨论了错误以及如何处理错误你读过那一章吗?因为你没有处理错误。查看C++关键词<代码>尝试和<代码> catch <代码>。如果你不使用这些关键字,你的程序将在第一个异常结束。谢谢。