Is there any way to get some information at least for catch(…)?

前端 未结 5 592
太阳男子
太阳男子 2020-12-11 21:54

Is there any way to get at least some information inside of here?

...
catch(...)
{
  std::cerr << \"Unhandled exception\" << std::endl;
}
         


        
5条回答
  •  时光取名叫无心
    2020-12-11 22:16

    Yes there is, but how useful it is is open to debate:

    #include 
    #include 
    using namespace std;
    
    int f() {
        throw "message";
    }
    
    int main() {
        try {
            f();
        }
        catch ( ... ) {
            try {
                throw;
            }
            catch( const char *  s ) {
                cout << "caught " << s << endl;
            }
        }
    }
    

    And to actually to answer your question, IMHO you should always have a catch(...) at the top level of your code, that terminates (or otherwise handles) when presented with an unexpected exception in your application, in a manner fully documented by your application's manual.

提交回复
热议问题