I'm working through Bjarne Stroustrup's "Programming: Principles and Practice Using C++" and using the PPP_support.h file from the official website https://www.stroustrup.com/PPP_support.h
I am using Visual Studio as my IDE.
According to the book (Chapter 5, "Errors!"), the error() function should behave as follows:
"in PPP_support we supply an error() that by default terminates the program with a system error message plus the string we passed as an argument to error().
However, when I run my code, the program just crashes without displaying my custom message.
Here is a minimal, reproducible example:
#include "PPP.h"
int main()
{
error("This is my custom error message.");
}
What I expected:
I expected the console to print something like: "error: This is my custom error message."
What actually happened:
The program immediately crashes and the console window closes, or I get a generic system message that doesn't include my custom string. The final output I see in my console from Visual Studio is:
C:\dev\hello_world\bin\x64\Debug\hello_world.exe (process 31660) exited with code 0 (0x0). Press any key to close this window . . .
Why is my custom error message not being printed to the console? Is there a change I need to make to see it?
The file you are using throws C++ exceptions. There is no guarantee that Windows will choose to show you the message embedded in the thrown exception. If you want to see these messages, you will have to write your own try-catch block, print the error, and then exit.
PPP_EXPORT inline void error(const std::string& s) // error() simply disguises throws
{
throw std::runtime_error(s);
}