抛出异常并忽略"reaches end of non-void function"警告不好吗?
Is it bad to throw an exception and ignore "reaches end of non-void function" warnings?
我的代码:
QSerialPortInfo getCentralInfo()
{
const QList<QSerialPortInfo> infoList =
QSerialPortInfo::availablePorts();
for (auto it = infoList.cbegin();
it != infoList.cend(); ++it) {
if (it->manufacturer() == "1a86") {
happyTalk("Znaleziono centralkę.");
return *it;
}
}
escape("Nie znaleziono centralki.");
}
void escape(QString errorMsg) {
qDebug() << "[!] " << errorMsg;
throw;
}
void happyTalk(QString msg) {
qDebug() << "[u2713] " << msg;
}
这样停止应用程序是否优雅,为什么不优雅?我得到调试器警告,但我认为这里调试器是错误的。: -)
将[[noreturn]]
属性添加到escape
,并实际抛出一个对象
[[noreturn]]
告诉编译器,优化器和希望警告生成器escape
永远不会返回,所以它应该(在理想的世界中)阻止该错误。
问题是
throw;
并不表示"抛出一些异常"。它表示"重新抛出当前正在处理的异常"。如果你执行throw
没有活动异常,我相信它会调用std::terminate
来结束程序。
一种更优雅的处理方法是抛出一个异常(可能是std::logic_error
)来指示发生了一些不好的事情,并将控制转移到错误处理程序,或者通过调用exit
或abort
来退出程序。
希望这对你有帮助!
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- 抑制非平凡无用的警告"control may reach end of non-void function"
- 收到此警告" Control may reach end of non-void function "
- 即使我已经涵盖了所有情况,也会收到"control reaches end of non-void function"警告
- "if constexpr"与"try in constexpr function"警告的交互
- 虚假"<function> is expected to return a value"警告?
- 当调用带有警告"control reaches end of non-void function"的函数时,(实际上)会发生什么?
- "Control may reach end of non-void function"警告
- GCC - 删除"is used uninitialized in this function"警告
- 为什么'control reaches end of non-void function'只是一个警告?合法吗?
- 如何摆脱C++生成器警告"virtual function hides..."
- "control reaches end of non-void function" Eclipse C++ 中的警告,但没有编译或运行时错误
- 禁用"bad function cast"警告
- 抛出异常并忽略"reaches end of non-void function"警告不好吗?
- if-else 块引发"control reaches end of non-void function"警告
- 双重调度会产生'hides virtual function'警告,为什么?