Exceptions - Incorrect behavior in VC ++ 2015 CTP Ultimate

I have a program:

#include<iostream> using namespace std; class Test { public: void func() { cout << "Inside func" << endl; throw; } }; int myfunc() { Test T; T.func(); return 1; } int main() { myfunc(); cout << "Main func" << endl;//should not print getchar(); } 

I expected this program to end with main , but in VC ++ 2015, main cout is printed. This was against my understanding, so I compiled it with gcc and it works fine there.

Is this a bug in VC ++ 2015, or is the program termination behavior as unspecified / UB behavior? If he ever performed cout << "Main func" << endl; ?

IDE: VS2015 CTP Preview (within 30 days)

flags: /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc140.pdb" /fp:precise /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\exception.pch"

+5
source share
2 answers

throw with no argument when called in the wrong context should call terminate .

In accordance with the standard:

An explication expression without operand causes the exception to be thrown to be thrown

....

If no exception is currently thrown, executing the throw expression without calling the operands std::terminate()

Then the behavior depends on the currently installed std::terminate_handler , but in any case, execution must be completed.

Mandatory behavior: A terminate_handler terminates the program without returning to the caller.

Default behavior: By default, terminate_handler calls abort . The default implementation calls std::abort .

+5
source

I assume that you are running your test program in the VS debugging environment. Try to compile your program in the release build and run it by clicking on the executable file. An error window will appear and "Main func" will not be printed. In fact, the behavior in the VS debugger is a function, not an error.

Microsoft states here that:

In Visual Studio, when exceptions are thrown or end up unhandled, the debugger can help you debug them, crashing like they break when a breakpoint is hit.

At this point, the developer may decide what goes wrong, or continue execution, which will lead to the behavior that the poster sees, i.e. listing of main func in his question.

+1
source

All Articles