I have a class that can throw an exception inside the constructor. I do not know own the code for this class so I cannot change this behavior or add other instantiation or initialization methods to this class. I need to create an object of this class inside of main. Does this mean that I need to have a main() that consists mostly of one giant try / catch block like this:
main()
{
try
{
A a;
...
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
What if this main is thousands of lines long? This try / catch block is huge. I feel like there should be a better way of doing this but I cannot think of one.
What if this main is thousands of lines long?
... I feel like there should be a better way of doing this but I cannot think of one.
That's clearly a sign for bad design and should be refactored into classes and function calls.
Ideally to something like this in main()
:
int main(int argc, char* argv[]) {
try {
Application app(argc,argv);
app.run();
}
catch(std::exception& e) {
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Still the
try {
// Refactored 1000 lines of code
}
catch(std::exception& e) {
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
}
must enclose the calling code.