开发者

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 thes开发者_如何学Pythone 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.


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.


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


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.


The macros were important for use with an earlier version of the compiler. They are still supported for compatibility with legacy code, but otherwise are not recommended for use.

The documentation for Exception Handling in Visual C++ says this about MFC exceptions:

Since version 3.0, MFC has used C++ exceptions but still supports its older exception handling macros, which are similar to C++ exceptions in form. Although these macros are not recommended for new programming, they are still supported for backward compatibility. In programs that already use the macros, you can freely use C++ exceptions as well. During preprocessing, the macros evaluate to the exception handling keywords defined in the Visual C++ implementation of the C++ language as of Visual C++ version 2.0. You can leave existing exception macros in place while you begin to use C++ exceptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜