【发布时间】:2016-03-07 19:28:57
【问题描述】:
我正在从 Visual Studio 2013 更新到 Visual Studio 2015,并注意到这种行为差异。
#include <stdexcept>
#include <WinSock2.h>
#include <ws2tcpip.h>
int main()
{
WORD version = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(version, &wsaData) != 0)
{
throw std::runtime_error("This one is not thrown");
}
WSASetLastError(1);
if (WSAGetLastError() != 1)
{
throw std::runtime_error("This one neither");
}
#if 1
std::runtime_error test("an error");
#endif
if (WSAGetLastError() != 1)
{
throw std::runtime_error("This is thrown when the above code path is enabled");
}
return 0;
}
当我启用 std::runtime_error 代码路径时,WSAGetLastError 标志被重置。禁用它,程序返回 0 没有任何问题。
【问题讨论】:
-
你在哪里给
WSAStartup打电话?阅读文档:msdn.microsoft.com/en-us/library/windows/desktop/… -
我删除了所有对于重现问题不是必需的代码,以使源代码对堆栈溢出更加友好。我最初有 WSAStartup 并且行为是相同的。
-
修复它以避免将焦点从实际问题上移开
标签: c++ visual-studio exception visual-studio-2015 winsock2