【发布时间】:2018-05-28 18:57:05
【问题描述】:
我有以下代码:
void App::start()
try
{
initialize();
//...
m_errorCode = 0;
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << '\n';
m_errorCode = -1;
}
catch (...)
{
std::cerr << "Unknown exception\n";
m_errorCode = -2;
}
void App::initialize()
{
m_controller = createController();
//...
}
std::unique_ptr<IController> App::createController() const
{
if (m_config.m_controllerType == "iot_v1")
{
return std::make_unique<ControllerIotV1>();
}
if (m_config.m_controllerType == "iot_v2")
{
return std::make_unique<ControllerIotV2>();
}
throw new std::invalid_argument("Unsupported controller type.");
}
我无法在catch (const std::exception &ex) 块中捕获std::invalid_argument。而是触发了 catch(...) 块。但据我所知,std::invalid_argument 继承自 std::exception 并且应该可以被第一个块捕获。是吗?我觉得我错过了一些明显的东西。
【问题讨论】:
-
您缺少的主要是一个最小的、完整的示例,它可以编译、运行和演示问题。
-
了解
new的作用。 -
const std::exception &ex≠std::exception const* ex.