As we known, try...catch mechanism is a quite common feature for the high level languages like java or C#. Although C++ proclaimed that it supports this mechanism, the memory management limitation of C++ makes this try...catch function is weak.
C++ cannot automatically captures the exception like C# or Java, so in case of exception happens, we must throw explicitly.
// exceptions#include <iostream>usingnamespacestd;
intmain () {
Char* pEx = 0;
...
try {
if(pEx == 0)
throw20; }
catch(
inte) { cout <<
"An exception occurred. Exception Nr. "<< e <<
‘\n‘; }
return 0;
}
But if we are coding with managed and unmanaged code, how to handle the exception?
Actually for the managed part try block no doubt can capture the exception, for the un managed part we can use one special way.
Try catching using the ExternalException class
The base exception type for all COM interop exceptions and structured exception handling (SEH) exceptions.
[SerializableAttribute][ComVisibleAttribute(true)]
public ref class ExternalException : public SystemException
As the sample above, makes your target clause derived from ExternalException class, then internally the function can capture the exception.