简体   繁体   中英

catching exceptions in C/C++ while using cURL lib

I am working on a C++ program, which uses cURL library, which is written in plain C. When I try to connect to an incorrect URL address with cURL handle I get such an exception :

terminate called after throwing an instance of 'std::logic_error'
what():  basic_string::_S_construct NULL not valid

and my program terminates instead of skipping this URL and going futher. Here is snippet of my code:

CURL* curl;
curl_easy_setopt( curl, CURLOPT_URL, "incorrect URL" );

curl_easy_perform( curl ); // this method throws the expection

I tried to handle it like this :

try{
   curl_easy_perform( curl ); 
} catch { std::logic_error &e){
    return -1; // skip this URL and go futher
}

But still program terminates and it seems that exception is not handled properly.

The file "stdexcept" is included.

Does anyone knows some more about this error and how to catch this exception properly so I my program can keep on working?

I'm not a libcurl expert, but don't you need to assign the result of curl_easy_init() to your curl variable before calling the next two curl functions?

ETA, the following code does not throw an exception for me. The curl_easy_perform return is CURLE_COULDNT_RESOLVE_HOST (6) .

#include <curl/curl.h>
#include <iostream>
int main()
{
    CURL* curl = curl_easy_init();
    std::cout << curl_easy_setopt(curl, CURLOPT_URL, "incorrect URL") << std::endl;
    std::cout << curl_easy_perform(curl) <<std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM