Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throwing a run time error

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.

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.

#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.."

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

like image 526
stanna23 Avatar asked Oct 03 '14 00:10

stanna23


2 Answers

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:

#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;
}
like image 59
Gillespie Avatar answered Sep 19 '22 05:09

Gillespie


First, I don't know how your program is compiling, you need to include 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:

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.

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.

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

 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;
}
like image 43
Carl Avatar answered Sep 18 '22 05:09

Carl