如题

解决方案 »

  1.   

    try{ //用于捕获异常}
    catch(){//用于处理异常
    }
      

  2.   

    TRY
    {
    CString str; 
    CArchive ar(&file, bStoring ? CArchive::store : CArchive::load);

    if (bStoring)
    {
    // save signature
    ar << CString(lpszSignature); // Save the changed user details
    for (int i=0; i < m_UserArray.GetSize(); i++)
    {
    m_UserArray[i].Serialize(ar);
    } ar.Flush();
    }
    else
    {
    // load signature
    ar >> str;
    // if this the file we are looking for ?
    if (str.Compare(lpszSignature) == 0)
    {
    int nCount=0; while(!ar.IsBufferEmpty())
    {
    CUser user; // get user data
    user.Serialize(ar);

    // add user to array
    m_UserArray.Add(user);
    }
    }
    }
    ar.Close();
    file.Close();
    }
    CATCH_ALL(e)
    {
    // catch all exceptions that might happen ...
    return FALSE;
    }
    END_CATCH_ALL
      

  3.   

    try
    {
    // Attach the socket handle to a CSocket object.
    // This makes sure that the socket notifications are sent to this thread.
    m_ConnectSocket.Attach(m_hSocket);
    m_ConnectSocket.m_pThread = this; CString strIPAddress;
    UINT nPort;
    m_ConnectSocket.GetPeerName(strIPAddress, nPort); // notify server that there's a new connection
    m_pWndServer->SendMessage(WM_THREADSTART, (WPARAM)this, 0);

    if (((CFTPServer *)m_pWndServer)->CheckMaxUsers())
    {
    m_ConnectSocket.SendResponse("421 Too many users are connected, please try again later.");
    PostThreadMessage(WM_QUIT,0,0);
    }
    else
    if (!((CFTPServer *)m_pWndServer)->IsIPAddressAllowed(strIPAddress))
    {
    m_ConnectSocket.SendResponse("421 Access denied, IP address was rejected by the server.");
    PostThreadMessage(WM_QUIT,0,0);
    }
    else
    {
    // send welcome message to client
    CString strText = ((CFTPServer *)m_pWndServer)->GetWelcomeMessage();
    m_ConnectSocket.SendResponse("220 " + strText);
    m_nTimerID = ::SetTimer(NULL, 0, 1000, TimerProc); 
    }
    }
    catch(CException *e) 
    {
    e->Delete();
    }
    return TRUE;
      

  4.   

    程序中用TRY来定义一个进程模块.  TRY 进程模块定义了一系列语句,这些语句有可能抛出异常. 这些异常用来在之后的CATCH and AND_CATCH 中进行接收处理. TRY模式在递归中也允许进行: 异常可以抛给一个外部的TRY, 既可以忽略他们可以交给THROW_LAST 模块进行处理. 如果要结束 TRY 模块需要使用 END_CATCH 或者对应的END_CATCH_ALL 模块.
    各个语句参量定义如下:
    CATCH( exception_class, exception_object_pointer_name )
    AND_CATCH( exception_class, exception_object_pointer_name )
    THROW( exception_object_pointer )