views:

1632

answers:

8

in c++, Iam trying to catch all types of exceptions in one catch (like catch(Exception) in C#). how is it done ? and more, how can one catch devide-by-zero exceptions ?

+10  A: 
catch (...)
{
   // Handle exceptions not covered.
}

Important considerations:

  • A better approach is to catch specific types of exception that you can actually recover from as opposed to all possible exceptions.
  • catch(...) will also catch certain serious system level exceptions (varies depending on compiler) that you are not going to be able to recover reliably from. Catching them in this way and then swallowing them and continuing could cause further serious problems in your program.
  • Depending on your context it can be acceptable to use catch(...), providing the exception is re-thrown. In this case, you log all useful local state information and then re-throw the exception to allow it to propagate up. However you should read up on the RAII pattern if you choose this route.
Ash
+2  A: 

Make all your custom exception classes inherit from std::exception, then you can simply catch std::exception. Here is some example code:

class WidgetError
    : public std::exception
{
public:
    WidgetError()
    { }

    virtual ~WidgetError() throw()
    { }

    virtual const char *what() const throw()
    {
     return "You got you a widget error!";
    }
};
Adam Pierce
A: 

If I recall correctly (it's been a while since I've looked at C++), I think the following should do the trick

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

and a quick google(http://www.oreillynet.com/pub/a/network/2003/05/05/cpluspocketref.html) seems to prove me correct.

Paul Wicks
+1  A: 

In C++, the standard does not define a divide-by-zero exception, and implementations tend to not throw them.

hazzen
+9  A: 

You don't want to be using catch (...) (i.e. catch with the ellipsis) unless you really, definitely, most provable have a need for it.

The reason for this is that some compilers (Visual C++ 6 to name the most common) also turn errors like segmentation faults and other really bad conditions into exceptions that you can gladly handle using catch (...). This is very bad, because you don't see the crashes anymore.

And technically, yes, you can also catch division by zero (you'll have to "StackOverflow" for that), but you really should be avoiding making such divisions in the first place.

Instead, do the following:

  • If you actually know what kind of exception(s) to expect, catch those types and no more, and
  • If you need to throw exceptions yourself, and need to catch all the exceptions you will throw, make these exceptions derive from std::exception (as Adam Pierce suggested) and catch that.

Cheers,

Carl

Carl Seleborg
+1  A: 

If catching all exceptions - including OS ones - is really what you need, you need to take a look at your compiler and OS. For example, on Windows you probably have "__try" keyword or compiler switch to make "try/catch" catch SEH exceptions, or both.

ima
ebel gil : thank you, your answer helped a lot !
gil
A: 

You can, of course, use "catch (...) { /* code here */ }", but it really Depends On What You Want To Do. In C++ you have deterministic destructors (none of that finalisation rubbish), so if you want to mop up, the correct thing to do is to use RAII.

For example. instead of:

void myfunc() { void* h = get_handle_that_must_be_released(); try { random_func(h); } catch (...) { release_object(h); throw; } release_object(h);

}

Do something like:

#include <boost/shared_ptr.hpp>

void my_func() { boost::shared_ptr h(get_handle_that_must_be_released(), release_object); random_func(h.get()); }

Create your own class with a destructor if you don't use boost.

+2  A: 

If you are on windows and need to handle errors like divide by zero and access violation you can use a structured exception translator. And then inside of your translator you can throw a c++ exception:

void myTranslator(unsigned code, EXCEPTION_POINTERS*)
{
    throw std::exception(<appropriate string here>);
}

_set_se_translator(myTranslator);

Note, the code will tell you what the error was. Also you need to compile with the /EHa option (C/C++ -> Code Generatrion -> Enable C/C++ Exceptions = Yes with SEH Exceptions).

If that doesn't make sense checkout the docs for _set_se_translator

Matt Price