Is there a case where using catch all clause: catch (...) is justified?

Every time I saw the catch all expression:

try 
{
  // some code 
}
catch (...)
{

}

he always had abuse.

Arguments against the use of the cache all reservations are obvious. He will catch everything, including the OS, except in cases such as access violations. Since the exception handler cannot know what it is dealing with, in most cases the exceptions will appear as obscure log messages or some incoherent messages.

So catch(...)it seems an inherent evil.

But it is still implemented in C ++ and other languages ​​(Java, C #) implements similar mechanisms. So are there any cases when its use is justified?

+5
8

, -, , . , , - .

, ... .

catch (...) , , ( , ..).

, catch (...) . - , , , - , , . , .

, ; , . (...) .

+4

(1) , . " " Windows; MSV++.

(2) , catch-all . . . , .

+6

, , ( - ) , .

++, , catch(...) , , , RAII. , -, .

+5

, , ++:

, std:: terminate() ; , std:: terminate() - - .

(15.3/9)

, main() catch-all; , , .

+3

catch(...) , ( )

-, . , std::exception, No-No, main():

int execute(void); // real program lies here

int main(void)
{
    try
    {
        return execute();
    }
    catch(const std::exception& e)
    {
        // or similar
        std::cerr << "Unhandled exception: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    catch(...)
    {
        std::cerr << "Unknown exception!" << std::endl;
        return EXIT_FAILURE;
    }
}

" ", . - catch, , - Bad Thing. , ; "- , , !" , .

- , , . , RAII. , , - , , .

+2
  • try {...} catch (...) , ++ ( C-).

    , - ++ , std:: exception, , , . , ( " , ", ++).

  • . , 1. , .

+2

catch (...) , , , , . , , . , , .

+1

catch(...) finally, :

try {
  ...
} catch(...) {
  cleanup...
  throw;
}

- "" - . API C .

This is also useful with respect to plugin code that you do not control or simply do not trust in terms of stability. This will not stop their failure, but it can aggravate the situation.

Finally, there are times when you really don't care about the outcome of something.

-2
source

All Articles