VC++ Exceptions

Handling managed exceptions in VC++ is basically the same as C#. VC++ using managed extensions exposes some features of exceptions that are not available in C#. Those differences will be illustrated later. Listing 15.12 shows how to define and use a custom exception class. The full source for this sample can be found in C++Exceptions\BasicExceptions.

Listing 15.12. Basic Managed VC++ Exceptions
__gc class MyException : public System::ApplicationException
{
public:
    MyException() : System::ApplicationException()
    {
    }
    MyException(System::String __gc *msg) : System::ApplicationException(msg)
    {
    }
} ;
void ThrowMyException()
{
    throw new MyException(new System::String("This exception is thrown from
 ThrowMyException")); } int wmain(void) ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.