【问题标题】:exception while thread is throwing error in scoket c++ windows线程在套接字 C++ 窗口中抛出错误时出现异常
【发布时间】:2011-09-08 13:26:14
【问题描述】:

我正在使用套接字在 Windows 中创建客户端服务器应用程序,如果发生任何问题,我想在运行时从线程抛出异常,但我收到 throw 语句错误。

    //create thread in cpp file
    CreateThread(NULL,0,startServer,this,0,NULL);

        //thread in header file
            static unsigned long __stdcall startServer(void *i_SocketTransportServer)
           {
                           ((SocketTransportServer*)i_SocketTransportServer)->StartServerThread(((SocketTransportServer *)i_SocketTransportServer)->m_socketServer);
                return 0;
           }

        //and StartServerThread is function called by thread
         // SocketTransportServer is inner class of  RMLThinTransport
            void RMLThinTransport::SocketTransportServer::StartServerThread(SOCKET i_socketServer)
            {
                m_socketAccept=NULL;
                while(true)
                {
                    Sleep(20);
                    if(m_canAcceptMore)
                    {
                        m_canAcceptMore=false;
                        if(!m_isRunning)
                        {
                                break;
                        }
                        try
                        {
                            m_socketAccept=accept(m_socketServer,NULL,NULL);
                            if(m_socketAccept==INVALID_SOCKET)
                            {
                                int lastError=WSAGetLastError();
                                closesocket(m_socketAccept);
                                                                                                                            SocketExceptions                                        
 exceptionInAcceptAtServer;
                                  exceptionInAcceptAtServer.detectErrorAccept(&lastError);

throw exceptionInAcceptAtServer;
                            }
                            else
                            {
                                //_LOG("Client connected",EventTypeInfo) ;
                                OutputDebugStringW(L"client connected.....");
                                /* If client connected then setClinetCout value 1 */
                                setClientCount(1);
                                m_ClientSockets.push_back(m_socketAccept);
                                CreateThread(NULL,0,receiveDataAtServer,this,0,NULL);
                            }

                        }
                        catch(SocketExceptions& i_exceptionInAcceptAtServer)
                        {   

                            /*OutputDebugStringW(L"Can't accept client In Exception. ."); */
    throw i_exceptionInAcceptAtServer;//getting runtime error from here

                        }
                    }

                }

            }

现在我想在服务器关闭时抛出错误,但出现运行时错误。那么有什么办法可以让我的主函数出错。对不起,但我是 C++ 新手,所以请帮助我。错误是

【问题讨论】:

    标签: c++ windows sockets exception-handling


    【解决方案1】:

    抛出异常的代码不是问题;问题在于缺少任何代码来捕获异常。应用程序正在终止,因为没有任何东西捕捉到您抛出的异常;你必须确保有东西能抓住它。您的startServer 方法——线程过程——必须捕获异常,并干净地退出线程。

    【讨论】:

    • 我想在线程之外捕获异常,而不是在线程之外。
    • 你不能这样做,异常不会从一个线程传播到另一个线程。它们展开该单个线程的堆栈,如果它们在没有被捕获的情况下到达顶部,则您的应用程序将终止。您可以做的是捕获异常,并在您的 catch 块中使用其他机制将问题传达给应用程序的其余部分。
    • 捕获线程上的异常,然后通过您定义的某种方式将其转移到其他地方。 (例如,向您的主线程发送消息。)
    • 好的,我会尝试这样做,但如果您有任何相关示例,请告诉我。谢谢
    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多