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.
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'.
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.
So, try... catch can only handle errors that occur in valid code.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With