[英]catching exceptions in C/C++ while using cURL lib
我正在使用CURL库编写C ++程序,该库是用纯C语言编写的。当我尝试使用cURL句柄连接到不正确的URL地址时,出现了这样的异常:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
并且我的程序终止,而不是跳过此URL并继续。 这是我的代码的片段:
CURL* curl;
curl_easy_setopt( curl, CURLOPT_URL, "incorrect URL" );
curl_easy_perform( curl ); // this method throws the expection
我试图这样处理:
try{
curl_easy_perform( curl );
} catch { std::logic_error &e){
return -1; // skip this URL and go futher
}
但是程序仍然终止,并且似乎异常处理不正确。
包含文件“ stdexcept”。
有谁知道关于此错误的更多信息以及如何正确捕获此异常,以便我的程序可以继续工作?
我不是libcurl专家,但是在调用接下来的两个curl函数之前,是否不需要将curl_easy_init()
的结果分配给curl
变量?
预计,以下代码不会对我引发异常。 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.