C++ 无法捕获'catch(const std::exception&;ex)中的std::invalid_参数`
我有以下代码:C++ 无法捕获'catch(const std::exception&;ex)中的std::invalid_参数`,c++,exception,try-catch,C++,Exception,Try Catch,我有以下代码: 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
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.");
}
void应用程序::start()
尝试
{
初始化();
//...
m_errorCode=0;
}
捕获(const std::exception&ex)
{
std::cerr您应该按值抛出(无新的new
):
您缺少的主要内容是编译、运行和演示问题的最小完整示例。请阅读new
的功能。const std::exception&ex
≠ <代码>标准::异常常量*ex
。
throw std::invalid_argument("Unsupported controller type.");