views:

264

answers:

3

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.

+5  A: 

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.

BlueRaja - Danny Pflughoeft
but how does it compare to using the built-in try/catch?
Tim
@Tim: They only exist because MFC 1.0 didn't support the built-in try/catch. So just don't use them. Take a look at the macro definitions if you care to see how they work.
BlueRaja - Danny Pflughoeft
"Don't use them" sounds like the right path. From a business perspective though (sorry), is that the same advice Microsoft gives? Since they're the makers of MFC, I'd like to have an official recommendation to present to others.
Tim
@Tim: http://msdn.microsoft.com/en-us/library/t078xe4f%28v=VS.80%29.aspx `If you're writing a new application using MFC, you should use the C++ mechanism.`
BlueRaja - Danny Pflughoeft
+1  A: 

It's an artifact of Windows programming from before the days when try/catch wasn't well supported by compilers, or wasn't supported at all.

Chris Kaminski
+2  A: 

You'll want to keep in mind that there are 3 different kinds of exceptions when programming with Visual C++:

  1. C++ exceptions
  2. Structured exceptions (SEH, Windows' own exception mechanism)
  3. MFC exceptions (those you mention, which are not recommended for new code but can still be used for backwards compatibility)

SEH and C++ exception mechanisms should not be mixed.

This article on MSDN has more details:

http://msdn.microsoft.com/en-us/library/x057540h.aspx

jrfactotum
Thanks, that's good to know!
Tim