address_v4 :: from_string()崩溃了
address_v4 address = address_v4::from_string("");
提升版:1_53
答案 0 :(得分:3)
事实上并没有崩溃。它只是抛出一个例外记录:
#include <boost/asio.hpp>
#include <iostream>
int main() {
try {
auto address = boost::asio::ip::address_v4::from_string("");
} catch(std::exception const& e) {
std::cout << e.what() << "\n";
}
}
打印&#34;无效的参数&#34;。这是因为论证无效。 &#34;&#34;不是有效的地址。
您可以选择使用error_code
的重载来避免抛出异常:
<强> Live On Coliru 强>
#include <boost/asio.hpp>
#include <iostream>
int main() {
boost::system::error_code ec;
auto address = boost::asio::ip::address_v4::from_string("", ec);
if (!ec)
std::cout << "Address: " << address << '\n';
else
std::cout << "Error: " << ec.message();
}