I am working with code that has a lot of try
catch
blocks in but most cases the catch
blocks do nothing. As in the code below fib
function is throwing invalid_argument
exception. The function call in main
is in the try
block but the catch
block does not do anything, except catching the exception.
I am wondering if the compiler might trim away this kind of exception handling during code optimization, or not?
#include <iostream>
#include <exception>
// Declaration for Wmissing-declarations flag
int fib(int);
int fib(int n)
{
if (n < 0)
{
throw std::invalid_argument("Invalid argument");
}
if (n == 0 || n == 1)
return n;
return fib(n-1) + fib(n-2);
}
int main(int argc, char *argv[])
{
int _number;
std::cin >> _number;
try
{
std::cout << fib(_number) << std::endl;
}
catch(const std::invalid_argument & e)
{
}
return 0;
}
Compiling above code with most (all I know) flags turned on, as below, does not show any warning.
g++ -o except exceptions.cxx -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror -Wno-unused
Aucun commentaire:
Enregistrer un commentaire