Thread: How to use exception handling?

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    1

    How to use exception handling?

    I have a code which gets public IP address and one segment of code causes crash if not connected to internet. So I'm trying to apply exception handling, but I don't really understand how it works in this case, can you help me?
    Code:
    #include <string.h>
    #include <winsock2.h>
    #include <windows.h>
    #include <iostream>
    #include <vector>
    #include <locale>
    #include <sstream>
    using namespace std;
    #pragma comment(lib,"ws2_32.lib")
    
    
    string website_HTML;
    locale local;
    void get_Website(string url);
    char lineBuffer[200][80] = { ' ' };
    char buffer[10000];
    char ip_address[16];
    int i = 0, bufLen = 0, j = 0, lineCount = 0;
    int lineIndex = 0, posIndex = 0;
    
    //****************************************************
    
    int main(void) {
        cout << "\n\n\n";
        try{
        get_Website("api.ipify.org");
        }
        catch (const char* error)
        {
    
        }
        for (size_t i = 0; i < website_HTML.length(); ++i) website_HTML[i] = tolower(website_HTML[i], local);
    
        istringstream ss(website_HTML);
        string stoken;
    
        while (getline(ss, stoken, '\n')) {
    
            //cout <<"-->"<< stoken.c_str() << '\n';
    
            strcpy_s(lineBuffer[lineIndex], stoken.c_str());
            int dot = 0;
            for (int ii = 0; ii < strlen(lineBuffer[lineIndex]); ii++) {
    
                if (lineBuffer[lineIndex][ii] == '.') dot++;
                if (dot >= 3) {
                    dot = 0;
                    strcpy_s(ip_address, lineBuffer[lineIndex]);
                }
            }
    
            lineIndex++;
        }
        cout << "Your IP Address is  " << ip_address << " \n\n";
    
    
        cout << "\nPress ANY key to close.\n\n";
        cin.ignore(); cin.get();
    
        return 0;
    }
    
    //****************************************************
    
    void get_Website(string url) {
        WSADATA wsaData;
        SOCKET Socket;
        SOCKADDR_IN SockAddr;
        int lineCount = 0;
        int rowCount = 0;
        struct hostent *host;
        string get_http;
    
    
        get_http = "GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
    
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
            cout << "WSAStartup failed.\n";
            system("pause");
            //return 1;
        }
    
        Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        host = gethostbyname(url.c_str());
    
        SockAddr.sin_port = htons(80);
        SockAddr.sin_family = AF_INET;
        SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    
        if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0) {
            cout << "Could not connect";
            system("pause");
            //return 1;
        }
        send(Socket, get_http.c_str(), strlen(get_http.c_str()), 0);
    
        int nDataLength;
        while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0) {
            int i = 0;
            while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
    
                website_HTML += buffer[i];
                i += 1;
            }
        }
    
        closesocket(Socket);
        WSACleanup();
    
    }

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    803
    e.g.
    Code:
    #include <stdexcept>
    
    void get_Website(const std::string& url)
    {
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
            throw std::runtime_error("WSAStartup failed");
        }
    }
    
    int main()
    {
        try {
            get_Website("api.ipify.org");
        } catch (const std::exception& e) {
            std::cerr << e.what() << std::endl;
        } catch (...) {
            std::cerr << "Unknown exception" << std::endl;
        }
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception errors OR exception handling :S
    By cruiser in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2011, 05:30 AM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. Exception handling !!!
    By Brain Cell in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 06:25 PM
  5. Exception handling
    By kuwait in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 06:20 AM

Tags for this Thread