一种错误处理的写法(学习)


CString IOCPS::ErrorCode2Text(DWORD dw)
{
    CString error="";
    // Put your own common error text here (give more explaination etc..)
    switch(dw)
    {
    case WSAEFAULT:
        error="WSAEFAULT    The buf parameter is not completely contained in a valid part of the user address space.";
        break;
    case WSAENOTCONN:
        error="WSAENOTCONN    The socket is not connected.";
        break;
    case WSAEINTR:
        error="WSAEINTR    The (blocking) call was canceled through WSACancelBlockingCall.    ";
        break;
    case WSAENOTSOCK:
        error=" WSAENOTSOCK    The descriptor s is not a socket.";
        break;
    case WSANOTINITIALISED:
        error="WSANOTINITIALISED: A successful WSAStartup call must occur before using this function.";
        break;
    case WSAENETDOWN:
        error="WSAENETDOWN    The network subsystem has failed.";
        break;
    case WSAEINPROGRESS:
        error="WSAEINPROGRESS    A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.";
        break;
    case WSAENETRESET:
        error=" WSAENETRESET    The connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress.";
        break;
    case WSAEOPNOTSUPP:
        error="WSAEOPNOTSUPP    MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations.    ";
        break;
    case WSAESHUTDOWN:
        error="WSAESHUTDOWN    The socket has been shut down; it is not possible to receive on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH.";
        break;
    case WSAEWOULDBLOCK:
        error=" WSAEWOULDBLOCK    The socket is marked as nonblocking and the receive operation would block.    ";
        break;
    case WSAEMSGSIZE:
        error=" WSAENOTSOCK        The message was too large to fit into the specified buffer and was truncated.";
        break;
    case WSAEINVAL:
        error="WSAEINVAL    The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled or (for byte stream sockets only) len was zero or negative.    ";
    case WSAECONNABORTED:
        error="     WSAECONNABORTED    The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable.";
        break;
    case WSAETIMEDOUT:
        error="WSAETIMEDOUT    The connection has been dropped because of a network failure or because the peer system failed to respond.    ";
        break;
    case WSAECONNRESET:
        //error="WSAECONNRESET    The virtual circuit was reset by the remote side executing a hard or abortive close.";
        error="WSAECONNRESET Connection dropped..";
        break;

    default:
        error="";  
        break;
    }

    // Use system format..  
    if(error.IsEmpty())
    {
        LPVOID lpMsgBuf;
        error="";
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM,
            NULL,
            dw,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );
        error.Format("%s",lpMsgBuf);
        LocalFree(lpMsgBuf);
    }
    return error;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值