Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TRY/CATCH_ALL vs try/catch

I've been using c++ for a while, and I'm familiar with normal try/catch. However, I now find myself on Windows, coding in VisualStudio for COM development. Several parts of the code use things like:

TRY {
    ... do stuff
} CATCH_ALL(e) {
    ... issue a warning
}
END_CATCH_ALL;

What's the point of these macros? What benefit do they offer over the built-in try/catch?

I've tried googling this, but "try vs TRY" is hard to search for.

like image 523
Tim Avatar asked Apr 14 '10 21:04

Tim


People also ask

Which is better try-catch or if else?

In 'if-else', we have one else block corresponding to one if block. Or we need to define another condition with command 'else if'. In 'try-catch' we don't have to define each 'try' block with a 'catch' block. 'if-else' is less time consuming than 'try-catch'.

What can I use instead of try-catch?

If you've one if/else block instead of one try/catch block, and if an exceptions throws in the try/catch block, then the if/else block is faster (if/else block: around 0.0012 milliseconds, try/catch block: around 0.6664 milliseconds). If no exception is thrown with a try/catch block, then a try/catch block is faster.

Does try-catch catch all errors?

So, try... catch can only handle errors that occur in valid code.

Is try-catch good practice C++?

This is not good programming practice in C++ or in any other language. Silent failures are bad and will bite you sooner or later. If you are going to catch (...) the very least you should do is log that you are doing it. You may also need to delete some objects if you are not using RAII.


1 Answers

It's an MFC macro:
http://msdn.microsoft.com/en-us/library/t8dwzac0%28VS.71%29.aspx

This page says they're a remnant from MFC 1.0 - use normal C++ exceptions in new code:

MFC versions lower than 3.0 did not support the C++ exception mechanism. MFC provided macros to deal with exceptions.

like image 115
BlueRaja - Danny Pflughoeft Avatar answered Oct 03 '22 12:10

BlueRaja - Danny Pflughoeft