我有以下代碼(現在可編譯,這要歸功於@LokiAstari):C++不能趕上從curlpp拋出的Mac OS與克++異常 - MP-4.4
要編譯下面的代碼段中,最簡單的方法是要下載curlpp,編譯一下。然後用g ++ - mp-4.4(由macport安裝)編譯下面的代碼,鏈接到curl和curlpp。
如果localhost:8080不存在,它會崩潰(輸出:abort trap
)。 更改g ++ - mp-4.4到g ++,它是Xcode 4默認的一個,它工作正常。
#include <string>
#include <sstream>
#include <iostream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
int main(int, char **)
{
try
{
curlpp::Cleanup myCleanup;
{
std::ostringstream os;
os << curlpp::options::Url("http://localhost:8080/");
}
}
catch(curlpp::RuntimeError &e)
{
std::cout << e.what() << std::endl;
}
catch(curlpp::LogicError &e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
更新:
在Easy.cpp:
void
curlpp::Easy::perform()
{
mCurl->perform(); // The exception come from here. mCurl is a std::auto_ptr<internal::CurlHandle> mCurl;
}
......
std::ostream & operator<<(std::ostream & stream, const curlpp::Easy & request)
{
// Quick clone that doesn't copy options, only the curl handle.
curlpp::Easy r(request.getCurlHandle().clone());
r.setOpt(new curlpp::options::WriteStream(& stream));
r.perform(); // The exception come from here.
return stream;
}
在CurlHandle.cpp
:
void CurlHandle::perform()
{
CURLcode code;
code = curl_easy_perform(mCurl);
throwException();
libcurlRuntimeAssert(mErrorBuffer, code); //if we got an error
}
在Exception.cpp
:
void curlpp::libcurlRuntimeAssert(const std::string & reason, CURLcode code)
{
curlpp::libcurlRuntimeAssert(reason.c_str(), code);
}
void curlpp::libcurlRuntimeAssert(const char * reason, CURLcode code)
{
if (code != CURLE_OK)
throw curlpp::LibcurlRuntimeError(reason, code);
}
我追溯到代碼throw curlpp::LibcurlRuntimeError(reason, code);
,LibcurlRuntimeError
的構造函數很好,然後拋出,然後是SIGABRT。
堆棧跟蹤:
0 __kill 0 0x7fff8213d0b6
1 abort 0 0x7fff821dd9f6
2 uw_init_context_1 0 0x101663af2
3 _Unwind_Resume 0 0x101663f38
4 operator<< Easy.cpp 118 0x10009b699
5 operator<< Options.cpp 34 0x1000948d8
6 RoleCreationTest::loadPlayerAsyncTestCase rolecreationtest.cpp 147 0x10008c714
7 RoleCreationTest::qt_metacall moc_rolecreationtest.cpp 87 0x1000acab2
8 QMetaMethod::invoke 0 0x1012e6667
爲什麼不能通過* constant * reference來捕捉異常?另外,如果添加一個全部區塊會發生什麼? –
我在想'os << url'並不是你認爲它做的,即它不會拋出異常。嘗試使用調試器進入代碼。 –
我的猜測是,在你的調用堆棧的某個地方,有一個函數被定義爲'nothrow'或者一個拋出規範,它不包含你提到的異常。儘管@AndreaBergia比我更可能是正確的。 – Omnifarious