Condividi tramite


__try_cast 

Note   This topic applies only to version 1 of Managed Extensions for C++. This syntax should only be used to maintain version 1 code. See safe_cast for information on using the equivalent functionality in the new syntax.

Performs the specified cast or throws an exception if the cast fails.

__try_cast < type-id > ( expression )

Remarks

The __try_cast keyword (similar in behavior to dynamic_cast) provides support for automatically throwing an exception (of type System::InvalidCastException) whenever the specified casting operation fails.

The __try_cast keyword can be used during the testing phase of your application, automatically alerting you to possible casting failures.

When porting Managed Extensions for C++, replace __try_cast calls with safe_cast.

__try_cast does not work on casts of pointer to value types (__value), since it is not possible to check the types at runtime.

Example

In the following example, an attempt to cast a pointer (of Derived type) to another pointer (of MoreDerived type) is made. If the cast fails, it is caught and reported by the catch block:

// keyword__try_cast.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
using namespace System;

__gc struct Base {}; 
__gc struct Derived : Base {};
__gc struct MoreDerived : Derived {};

int main() {
   Base*bp = new Derived;
   try {
       MoreDerived* mdp = __try_cast<MoreDerived*>(bp);
   }
   catch(System::InvalidCastException*) {
       Console::WriteLine("Could not cast 'bp' to MoreDerived*");
   }
}

Output

Could not cast 'bp' to MoreDerived*