简体   繁体   English

使用cURL lib捕获C / C ++中的异常

[英]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 : 我正在使用CURL库编写C ++程序,该库是用纯C语言编写的。当我尝试使用cURL句柄连接到不正确的URL地址时,出现了这样的异常:

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. 并且我的程序终止,而不是跳过此URL并继续。 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. 包含文件“ stdexcept”。

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? 我不是libcurl专家,但是在调用接下来的两个curl函数之前,是否不需要将curl_easy_init()的结果分配给curl变量?

ETA, the following code does not throw an exception for me. 预计,以下代码不会对我引发异常。 The curl_easy_perform return is CURLE_COULDNT_RESOLVE_HOST (6) . curl_easy_perform返回值为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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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