<p>In C++, I'm trying to catch all types of exceptions in one catch (like <code>catch(Exception)</code> in C#). How is it done? And what's more, how can one catch divide-by-zero exceptions?</p>

[{'answer_id': 55863, 'author': 'Ash', 'author_id': 5023, 'author_profile': 'https://Stackoverflow.com/users/5023', 'pm_score': 6, 'selected': True, 'text': '<pre><code>catch (...)\n{\n   // Handle exceptions not covered.\n}\n</code></pre>\n\n<p>Important considerations:</p>\n\n<ul>\n<li>A better approach is to catch specific types of exception that you can actually recover from as opposed to all possible exceptions.</li>\n<li>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.</li>\n<li>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 <a href="http://en.wikipedia.org/wiki/Resource_acquisition_is_initialization" rel="noreferrer">RAII pattern</a> if you choose this route.</li>\n</ul>\n'}, {'answer_id': 55865, 'author': 'Adam Pierce', 'author_id': 5324, 'author_profile': 'https://Stackoverflow.com/users/5324', 'pm_score': 2, 'selected': False, 'text': '<p>Make all your custom exception classes inherit from std::exception, then you can simply catch std::exception. Here is some example code:</p>\n\n<pre><code>class WidgetError\n    : public std::exception\n{\npublic:\n    WidgetError()\n    { }\n\n    virtual ~WidgetError() throw()\n    { }\n\n    virtual const char *what() const throw()\n    {\n        return "You got you a widget error!";\n    }\n};\n</code></pre>\n'}, {'answer_id': 55866, 'author': 'Paul Wicks', 'author_id': 85, 'author_profile': 'https://Stackoverflow.com/users/85', 'pm_score': -1, 'selected': False, 'text': '<p>If I recall correctly (it\'s been a while since I\'ve looked at C++), I think the following should do the trick</p>\n\n<pre><code>try\n{\n // some code\n}\ncatch(...)\n{\n // catch anything\n}\n</code></pre>\n\n<p>and a quick google(<a href="http://www.oreillynet.com/pub/a/network/2003/05/05/cpluspocketref.html" rel="nofollow noreferrer">http://www.oreillynet.com/pub/a/network/2003/05/05/cpluspocketref.html</a>) seems to prove me correct.</p>\n'}, {'answer_id': 55867, 'author': 'hazzen', 'author_id': 5066, 'author_profile': 'https://Stackoverflow.com/users/5066', 'pm_score': 1, 'selected': False, 'text': '<p>In C++, the standard does not define a divide-by-zero exception, and implementations tend to not throw them.</p>\n'}, {'answer_id': 55916, 'author': 'Carl Seleborg', 'author_id': 2095, 'author_profile': 'https://Stackoverflow.com/users/2095', 'pm_score': 4, 'selected': False, 'text': '<p>You <strong>don\'t</strong> want to be using catch (...) (i.e. catch with the ellipsis) unless you really, definitely, most provable have a need for it.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>Instead, do the following:</p>\n\n<ul>\n<li>If you actually know what kind of exception(s) to expect, catch those types and no more, and</li>\n<li>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.</li>\n</ul>\n'}, {'answer_id': 55934, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 56274, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>You can, of course, use <code>catch (...) { /* code here */ }</code>, 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.</p>\n\n<p>For example. instead of:</p>\n\n<pre><code>void myfunc()\n{\n    void* h = get_handle_that_must_be_released();\n    try { random_func(h); }\n    catch (...) { release_object(h); throw; }\n    release_object(h);\n\n}\n</code></pre>\n\n<p>Do something like:</p>\n\n<pre><code>#include&lt;boost/shared_ptr.hpp&gt;\n\nvoid my_func()\n{\n    boost::shared_ptr&lt;void&gt; h(get_handle_that_must_be_released(), release_object);\n    random_func(h.get());\n}\n</code></pre>\n\n<p>Create your own class with a destructor if you don't use boost.</p>\n"}, {'answer_id': 57088, 'author': 'Matt Price', 'author_id': 852, 'author_profile': 'https://Stackoverflow.com/users/852', 'pm_score': 3, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>void myTranslator(unsigned code, EXCEPTION_POINTERS*)\n{\n    throw std::exception(&lt;appropriate string here&gt;);\n}\n\n_set_se_translator(myTranslator);\n</code></pre>\n\n<p>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).</p>\n\n<p>If that doesn\'t make sense checkout the docs for [_set_se_translator](<a href="http://msdn.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx)" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx)</a></p>\n'}, {'answer_id': 58223526, 'author': 'BuvinJ', 'author_id': 3220983, 'author_profile': 'https://Stackoverflow.com/users/3220983', 'pm_score': 0, 'selected': False, 'text': '<p>You can use <code>catch(...)</code> to catch EVERYTHING, but then <strong>you don\'t get a an object</strong> to inspect, rethrow, log, or do anything with exactly. So... you can "double up" the try block and rethrow into one outer catch that handles a single type.  This works ideally if you define constructors for a custom exception type that can build itself from all the kinds you want to group together. You can then throw a default constructed one from the <code>catch(...)</code>, which might have a message or code in it like "UNKNOWN", or however you want to track such things.</p>\n\n<p>Example:</p>\n\n<pre><code>try\n{ \n    try\n    {       \n        // do something that can produce various exception types\n    }\n    catch( const CustomExceptionA &amp;e ){ throw e; } \\\n    catch( const CustomExceptionB &amp;e ){ throw CustomExceptionA( e ); } \\\n    catch( const std::exception &amp;e )  { throw CustomExceptionA( e ); } \\\n    catch( ... )                      { throw CustomExceptionA(); } \\\n} \ncatch( const CustomExceptionA &amp;e ) \n{\n    // Handle any exception as CustomExceptionA\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/55859', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/195/']