'Getting a debug error using the function error()
This is a direct follow-up to my last question I asked recently. I would recommend checking it before reading this.
The text given below in blockquote and code sample is from a book called Programming: Principles and Practice using C++, Chapter 5- Errors (Under the Run-time errors section)
So, let’s tackle the problem of argument errors with area(). We have two obvious alternatives:
- Let the caller of area() deal with bad arguments.
- Let area() (the called function) deal with bad arguments.
Let’s try the first alternative (“Let the user beware!”) first. That’s the one we’d have to choose if area() was a function in a library where we couldn’t modify it. For better or worse, this is the most common approach. Protecting the call of area(x, y) in main() is relatively easy:
if (x<=0) error("non-positive x");
if (y<=0) error("non-positive y");
int area1 = area(x, y);
Really, the only question is what to do if we find an error. Here, we have called a function error() which we assume will do something sensible. In fact, in std_lib_facilities.h we supply an error() function that by default terminates the program with a system error message plus the string we passed as an argument to error(). If you prefer to write out your own error message or take other actions, you catch runtime_error (§5.6.2, §7.3, §7.8, §B.2.1). This approach suffices for most student programs and is an example of a style that can be used for more sophisticated error handling.
I tried to run the code sample in Visual Studio and this is what I was greeted to by a window of Microsoft C++ Runtime Library under practice34.exe:
Debug Error! Program:
C:\Users\..\source\repos\practice34\x64\Debug\practice34.exe
abort() has been called (Please Retry to debug the application)
I have options to either Abort, Retry or Ignore.
Retrying gives me this:
C:\Users\..\source\repos\practice34\x64\Debug\practice34.exe (process 9020) exited with code -2147483645.
Now I suppose, I should get a simple error in console using my string argument I passed in the error function. But instead I'm getting this. I have no idea why this is happening.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
