It is said that defining a function as noexcept will let the compiler do some optimizations to boost the program and if the function needs to throw the compiler will suppress that optimization.
Here's my simple snippet:
void func() noexcept(true){
std::vector<int> vec{2, 4, 6, 8}; // vec is a vector of 4 ints
std::cout << vec.at(5); // throws
}
int main(){
try{
func();
}
catch(std::exception const& e){
std::cout << e.what() << '\n';
}
catch(...){
//
}
}
- Why I cannot catch the exception? I am sure because the
noexceptexception specification. So what happens if a function is marked asnoexceptbut throws and how to catch its exception rather than letting the program callstd::terminate()to end the program?