我正在讨论C++异常并遇到一个错误,我不确定它为什么会给我一些问题:
#include <iostream>
#include <exception>
class err : public std::exception
{
public:
const char* what() const noexcept { return "error"; }
};
void f() throw()
{
throw err();
}
int main()
{
try
{
f();
}
catch (const err& e)
{
std::cout << e.what() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下运行时错误:
terminate called after throwing an instance of 'err'
what(): error
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
如果我try/catch完全移动逻辑f(),即
void f()
{
try
{
throw err();
}
catch (const err& e)
{ …Run Code Online (Sandbox Code Playgroud)