簡體   English   中英

使用cURL lib捕獲C / C ++中的異常

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM