throwing a run time error

0 votes

I just started Programming: Principles and Practice Using C++ because I'm new to programming. 

Errors and how to handle them are covered in one of the chapters.

What I'm attempting to implement is this little line of code. 

The programme will end with a system error message and the text we gave as a parameter, according to the book's description of 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;
}

"Unhandled exception at 0x7699c41f in test project.exe: Microsoft C++ exception: std::runtime error at memory position 0x0038fc18," is the message I receive.

Therefore, I want to know why the message I send to error() isn't being sent. What am I doing wrong?

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
368 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
707 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,280 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
534 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
487 views
0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
1,214 views
0 votes
1 answer

In C++, what is a virtual base class?

When employing multiple inheritance, virtual base classes are used to prevent several "instances" of a particular class from appearing in an inheritance hierarchy. Consider the following example: class A { public: void Foo() {} ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
528 views
0 votes
1 answer

Catch integer(0) in R

R's way of printing a zero length ...READ MORE

answered Apr 27, 2018 in Data Analytics by kappa3010
• 2,090 points
4,379 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
698 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,414 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,459 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP