1

If I run a simple app

#include <stdexcept>

int main() {
    throw std::runtime_error("Hello World!");
}

with Windows CMD, the error message is not shown. How can I fix it?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • There is no line of code that you posted that outputs anything. I don't see a `std::cout`, `printf` or any other output function. – PaulMcKenzie Aug 05 '22 at 13:19
  • 1
    @PaulMcKenzie I think OP was expecting the `throw` statement to give some output in the Windows CMD interface, as it would do if you ran it instead on an IDE. – Ryan Zhang Aug 05 '22 at 13:19
  • 1
    Well, that's the issue -- *expecting* a behavior that is not guaranteed to occur. If the OP wants guaranteed output, they need to use something guaranteed to give output, i.e. `cout`, `printf`, or similar function. A `throw`'s job is to throw an exception. It's the job of `catch` from that thrown exception to do the "output work". – PaulMcKenzie Aug 05 '22 at 13:22
  • @PaulMcKenzie this is an overly simplified example, in my real task when an exception occurs, I cannot see its message in CMD – Kaiyakha Aug 05 '22 at 13:23
  • It's demended to the user (the programmer) to manage the exception... in this case if you want an output to the screen then enclose inside a `try catch` block and in the `catch` do the output on the screen – Marco Beninca Aug 05 '22 at 13:24
  • @MarcoBeninca Doesn't matter how complex your code is the question remains. Are you printing anything to the console, or are you expecting it to happen automatically? If it is the latter then that is where your mistake is. – john Aug 05 '22 at 13:27
  • @john I didn't know `throw` does not guarantee to show the message – Kaiyakha Aug 05 '22 at 13:35
  • @Kaiyakha -- You could create your own exception class that outputs to the console on construction. But then the issue becomes -- what if the displaying of the message also throws an exception? – PaulMcKenzie Aug 05 '22 at 13:39

2 Answers2

7

Let's take a look at what throw does in C++ from the official Microsoft Docs.

In the C++ exception mechanism, control moves from the throw statement to the first catch statement that can handle the thrown type.

Note that this means throw does not actually output anything on its own — you'd have to catch it first, then output something. Also note that this means the throw will have to be surrounded by try if you want to do anything with the exception other than terminate the program (which throwing the exception will do on its own).

See below for an example of how to use throw properly.

#include <stdexcept>
#include <cstdio>

int main() {
    try {
        throw std::runtime_error("Hello World!");
    } catch (const std::exception& e) {
        puts(e.what());
    }
}
Ryan Zhang
  • 1,856
  • 9
  • 19
  • `puts` is not guaranteed to exist in the global namespace with `#include ` – Mestkon Aug 05 '22 at 13:35
  • You're right (in terms of the standard), but [it's fine in practice](https://stackoverflow.com/questions/32606023/when-using-c-headers-in-c-should-we-use-functions-from-std-or-the-global-na). – Ryan Zhang Aug 05 '22 at 13:38
3

The only thing guaranteed to happen when an exception escapes main is that the program stops.

To print the message of an exception you have to catch it and print the message

#include <stdexcept>
#include <cstdio>

int main()
{
    try {
        throw std::runtime_error("Hello World!");
    } catch (const std::exception& e) {
        std::puts(e.what());
    }
}
Mestkon
  • 3,532
  • 7
  • 18