How Can I Manage Try/Multiple Catch Blocks - Fix My Code

Apr 18, 2009 at 1:21am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
try
   {
        socketFileDescriptor = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
        if(socketFileDescriptor == INVALID_SOCKET )
        {
            cout<<"socket() error: " << WSAGetLastError() <<endl;
            throw 1;
        }

        cout<<"connecting to the server ..."<<endl;

        int connectStatus = connect(socketFileDescriptor, serverInfo->ai_addr, serverInfo->ai_addrlen);
        if(connectStatus == SOCKET_ERROR)
        {
            cout<<"connect() error: " << WSAGetLastError() <<endl;
            throw 2;
        }

        //send and receive messages
        while(true)
        {
            cout<<"\nyou: ";
            cin.getline(userInput, MAX_DATA);
            if(cin.fail())
            {
                cout<<"Overflow occured. Restart Application"<<endl;
                throw 2;
            }

            int bytes_sent = send(socketFileDescriptor, userInput, strlen(userInput), 0);
            while(bytes_sent != strlen(userInput))
            {
                if(bytes_sent == SOCKET_ERROR)
                {
                    cout<<"send() error: " << WSAGetLastError() <<endl;
                    throw 2;
                }

                //ask user to re-send again

            }

            int bytes_received = recv(socketFileDescriptor, serverResponse, MAX_DATA, 0);
            if((bytes_received == SOCKET_ERROR) && (bytes_received != 0))
            {
                cout<<"recv() error: " << WSAGetLastError() <<endl;
                throw 2;

            }else{
                cout<<"server shut down"<<endl;
                break;   //go to the end of main()
            }

            string echoedMsg(serverResponse, bytes_received);
            cout<<"server: "<< echoedMsg <<endl;
        } //end while()
    } //end try

    catch(1)
    {
        freeaddrinfo(serverInfo);
        WSACleanup();
        exit(-1);
    }

    catch(2)
    {
        closesocket(socketFileDescriptor);
        freeaddrinfo(serverInfo);
        WSACleanup();
        exit(-1);
    }

    closesocket(socketFileDescriptor);
    freeaddrinfo(serverInfo);
    WSACleanup();

    return 0;


Simple request: throw 1, should takes you to catch 1 & throw 2 should takes you to catch 2....How can i write the parameter of catch, and what should i write on "throw ??????? " ?

I hope my question is clear enough....Any help will be appreciated
Apr 18, 2009 at 2:25am
You don't catch individual values, you catch a var type of that value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
try
{
  if(badone)
    throw 1;
  if(badtwo)
    throw 2;
}
catch(int v)
{
  if(v == 2)
    closesocket(socketFileDescriptor);
  freeaddrinfo(serverInfo);
  WSACleanup();
  exit(-1);
}
Apr 19, 2009 at 1:01am
In general, it is best practice to create exception classes that inherit from one of the standard exceptions (e.g. std::runtime_error) rather than throwing POD types or even your own exception class. If that's too much work, then just throw std::runtime_error with an appropriate message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct ConnectionError : public std::runtime_error
{
   ConnectionError(const std::string& msg) : std::runtime_error(msg) {}
};

// define SocketError...

try
{
    socketFileDescriptor = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
    if(socketFileDescriptor == INVALID_SOCKET )
    {
        std::ostringstream err;
        err <<"socket() error: " << WSAGetLastError() <<std::endl;
        throw ConnectionError(err.str());
    }
    ....
}
catch (ConnectionError& ex)
{
    std::cerr << ex.what() << std::endl;
    ...
}


However, you are doing way too much in that one function. You could clean up some of the complexity with RAII.
Apr 22, 2009 at 6:00am
Thanks! The responses helped me
Topic archived. No new replies allowed.