I am going over c++ exceptions and am running into an error that I am unsure of why it is giving me issues:
#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;
}
}
When I run it, I get the following runtime error:
terminate called after throwing an instance of 'err'
what(): error
Aborted (core dumped)
If I move the try/catch logic completely to f(), i.e.
void f()
{
try
{
throw err();
}
catch (const err& e)
{
std::cout << e.what() << std::endl;
}
}
And just call it from main (without the try/catch block in main), then there is no error. Am I not understanding something, as it relates to throwing exceptions from functions?
Aucun commentaire:
Enregistrer un commentaire