Anders Lind
Anders Lind

Reputation: 4840

about c++ try -catch

I read code in a large project, that includs a lot of code like:

try
{
}
catch(...)
{
}

Literally, in the parenthesis after "catch", there is "..." in it. Not something like "exception e".

This makes me a little worry. Is this practice good or safe? thanks.

Upvotes: 2

Views: 1333

Answers (3)

bdonlan
bdonlan

Reputation: 231443

catch(...) catches all exceptions.

Normally, you would not want to do this. You have no idea what it is you just caught, and if you leave the catch block you have just silently ignored some kind of error. This could lead to very bad things happening later on. Since you have no idea what error just happened, you have no way to recover from it, and thus the only reasonable thing to do is to either allow the exception to continue (re-throw it) or abort the program's execution (call abort() or exit()).

However, if you have some cleanup you need to perform, it may be reasonable to catch all exceptions, perform the cleanup, then re-throw the exception:

try {
  // ...
} catch (...) {
  abortTransaction();
  throw;
}

That said, it's usually better to use so-called RAII classes to automate this cleanup:

DBTransaction txn = db.StartTransaction();

// do stuff that may throw; if it throws, txn will be destroyed,
// and its destructor can abort the transaction
// As such, an explicit try { } catch(...) { } isn't needed

Upvotes: 3

SpeedBirdNine
SpeedBirdNine

Reputation: 4676

It catches all exceptions. See this code:

try {
   throw CSomeOtherException();
}
catch(...) {  // Handle all exceptions
   // Respond (perhaps only partially) to exception
   throw;       // Pass exception to some other handler
}

From MSDN

You do not need to declare this parameter; in many cases it may be sufficient to notify the handler that a particular type of exception has occurred. However, if you do not declare an exception object in the exception-declaration, you will not have access to that object in the catch handler clause.

A throw-expression with no operand re-throws the exception currently being handled. Such an expression should appear only in a catch handler or in a function called from within a catch handler. The re-thrown exception object is the original exception object (not a copy). For example:

Hope this helps!

This practice is safe only if you have proper handlers written for exceptions. Avoiding it is better in my opinion.

Upvotes: 1

James McNellis
James McNellis

Reputation: 355307

No, this is a terrible practice.

If you catch(...), you have no idea what you've caught. It catches all C++ exceptions (and on some platforms with some settings, it catches other exceptions too, like structured exceptions in Visual C++).

If you have no idea what exception was thrown, you have no idea what the state of the system is: how do you know whether it is safe for the program to continue running?

The only two ways that it is absolutely safe to exit a catch(...) block is to terminate the program or to rethrow the exception (using throw;). The latter is occasionally useful if you need to perform some cleanup when an exception is thrown but can't rely on a destructor.

Upvotes: 12

Related Questions