Does it make sense to catch exceptions in the main(…)?

前端 未结 7 772
攒了一身酷
攒了一身酷 2020-12-16 17:50

I found some code in a project which looks like that :

int main(int argc, char *argv[])
{
  // some stuff

 try {
  theApp.Run();
 } catch (std::exception&a         


        
7条回答
  •  借酒劲吻你
    2020-12-16 18:02

    Have a look at the C++ bible i.e. Stroustrup, he has an example which is also repeated in Applied C++ programming. The reasoning is:

    int main(void)
    {
         try
         {
              // your code 
         }
         catch ( /* YourPossibleExceptions i.e. barfs you expect may occur */ )
         {
         }
         catch ( ... ) // unexpected errors, so you can exit gracefully
         {
         }
    }
    

提交回复
热议问题