Last Updated:

Exception handling in C++ | Visual C++ | CLR 

No doubt you're already familiar with the exception mechanism in standard C++, so you have a good understanding of how managed exceptions work. Recall that .NET (more specifically, the common language runtime (CLR) supports extensions that are compatible with the C++ Manageability Extension, and managed exceptions encountered when executing code generated in one of the .NET languages can be intercepted and processed by code written in any other .NET language.

In addition to handling predefined exceptions, such as Invalid-CastException or OverflowException, you can define your own Exception-derived classes that encapsulate some application-specific information. Consider the following example:

//Exceptions.cpp
#using <mscorlib.dll>
using namespace System;
// use namespace System;
_gc class MyException: public Exception
// garbage collector class MyException: public Exception
{
};
void TemperamentalFunction(int i) // hates odd numbers
{
Console::WriteLine(
"TemperamentalFunction called with {0}",
i.ToString());
if (i%2!= 0) // if (i%2!= 0), i.e. odd
throw new MyException;
Console::WriteLine("No exception thrown"); // No exception
}
void main()
{
try
{
TemperamentalFunction(2); // call with an even number
TemperamentalFunction(3); // call with an odd number
}
catch (MyException *pe)
{
Console::WriteLine("Exception thrown!"); // Exception!
Console::WriteLine(pe › get_StackTrace());
}
}

Here is the result of the program:

TemperamentalFunction called with 2
No exception thrown
TemperamentalFunction called with 3
Exception thrown!
at TemperamentalFunction(Int32 i) in с:\netcppcode\
chap03\exceptions\exceptions.cpp:line 16
at main() in c:\netcppcode\chap03\exceptions
\exceptions.cpp:line 25

Here is a more Russified version of this issue.

TemperamentalFunction called from 2
There is no exception
TemperamentalFunction called from 3
Exception!
to TemperamentalFunction(Int32 i) in c:\netcppcode\
chap03\exceptions\exceptions.cpp:line 16
in main() in c:\netcppcode\chap03\exceptions
\exceptions.cpp:line 25

Note the StackTrace method, which allows you to get a text string that represents the stack state at the time the exception occurred. Although this example does not use the _finally keyword (finally), be aware that such an extension to the C++ ANSI standard is supported in Visual C++. The _finally keyword (finally) allows you to insert code into your program that runs regardless of whether or not an exception occurs in the try block. It should also be mentioned that the _finally keyword is (finally) fully compatible with the exception mechanism supported by other .NET languages.

If desired, the previous example can be divided into two parts. The first part could be implemented in C# (as a dynamic link library (DLL)) and contain code that would throw an exception. The second part would be a C++ application calling the TemperamentalFunction method. In this way, you could clearly demonstrate that exceptions are indeed a bridge connecting different .NET languages.