4

So I am working on exception handling and I used a try-catch to solve an issue with converting strings to integers. The project runs fine, but if I look in the output window, multiple messages like this one show up:

Exception thrown at 0x00007FFB7ACE3B29 in CPP-eindopdracht.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x0000006DF9B3ECC0.

Does that mean my exception handling isn't working properly or is this always what the output window of VS2019 shows you anyway?

Thanks.

Edit:

Here is where the exception thrown message is generated when debugging:

bool Interpreter::tryConvertToInt(std::string convertible, int& value)
{
    try
    {
        value = std::stoi(convertible); // Also accepts "123abc" as a valid conversion. No problem for now but keep in mind.
        return true;
    }
    catch (...)
    {
        //std::cout << "Conversion from " << convertible << " to integer is not possible..." << std::endl;
        return false;
    }
}

However, as you can see I'm dealing with the exception, so I don't understand why the message is being shown in the output tab.

WolfyDoo
  • 143
  • 8
  • No. This is simply after I run the project. The project is functionally working but after running the project (not debug), and I open the output window, it shows the exception thrown message. – WolfyDoo Oct 14 '20 at 19:47
  • Well then use debug mode, that's what it's for – Expolarity Oct 14 '20 at 19:51
  • I edited my post with code. Could you take a look, please? – WolfyDoo Oct 14 '20 at 19:55
  • 3
    If you are running your code within the IDE debugger, even if you are not actively debugging the code, it still sees every exception being thrown and could simply be logging their existence whether they are actually caught or not. As long as the exception is not actually escaping your function, you are perfectly fine. – Remy Lebeau Oct 14 '20 at 20:05

2 Answers2

5

Your exception handling is working properly. From my tests, it looks like the Debug Output window in Visual Studio always shows the exceptions.

krisz
  • 2,686
  • 2
  • 11
  • 18
  • Thank you! It indeed only shows the exceptions in the output window after debugging, but also after just running the project. Is there a way to turn this off? It's so many exceptions that it becomes messy. – WolfyDoo Oct 14 '20 at 20:11
  • 2
    @TristanvO right click the output window, you can hide exceptions and other things too. – EDD Oct 14 '20 at 20:14
4

The Visual Studio debugger gives you options in how to handle exceptions. In Tools > Options > Debugging > Output Window, under General Output Settings, there's an On/Off option called "Exception Messages" which will inform you when exceptions are thrown.

On Windows, debuggers get two chances to deal with an exception. The "first chance" exception happens when an exception is first thrown. It gives the debugger an option to do something. Most Windows debuggers will, by default, report the exception and then continue.

If an exception isn't handled, the debugger gets a second chance, and this usually involves reporting that the exception wasn't caught and then stopping execution as if you'd hit a breakpoint.

In Debug > Windows, there's an Exception Settings window where you can control what happens happens for exceptions. There you can modify how the debugger handles the first chance exception. For example, you can have it stop execution when the exception is thrown before any unwinding and regardless of whether it'll be caught. You can also specify additional conditions to control whether it breaks or not.

Newer versions of Visual Studio seem to have moved away from the "first chance exception" terminology and now just say things like "break when exception is thrown."

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175