I was looking similar to finally
for c++ but I came across RAII. I have a small confusion though. If I have some common code I want to run in case of any exception,
Example: std::cout << "exception occured" << std::endl;
Is there a way to do that instead of copy the same code?
#include <iostream>
int main()
{
bool firstException = false;
try
{
if(firstException)
throw std::invalid_argument("the truth is out there!!");
else
throw std::domain_error("Bazzinga");
}
catch (std::invalid_argument const& e)
{
std::cout << e.what() << std::endl;
std::cout << "exception occured" << std::endl;
}
catch (std::domain_error const& e)
{
std::cout << e.what() << std::endl;
std::cout << "exception occured" << std::endl;
}
}