Mixing native and managed exceptions
I wrote that sample a while ago to better understand the mix of exception types:
#using <mscorlib.dll>
using namespace System ;
__value class ValueException
{
public:
ValueException( int i ) {}
} ;
__value class AnotherValueException
{
public:
AnotherValueException( int i ) {}
} ;
__gc class ReferenceException : public ApplicationException
{
} ;
__gc class AnotherReferenceException : public ApplicationException
{
} ;
void wmain()
{
for(;;)
{
Console::WriteLine( S"\n1. __box(ValueException(1))" ) ;
Console::WriteLine( S"2. __box(AnotherValueException(1))" ) ;
Console::WriteLine( S"3. new ReferenceException()" ) ;
Console::WriteLine( S"4. 3.1415" ) ;
Console::WriteLine( S"5. __nogc new AnotherValueException(1)" ) ;
Console::WriteLine( S"6. new AnotherReferenceException()" ) ;
Console::WriteLine( S"7. new int" ) ;
Console::WriteLine( S"8. new Int32" ) ;
String * c = Console::ReadLine() ;
Console::Write( S"\nCaught with " ) ;
try
{
switch ( Int32::Parse(c) )
{
case 1: throw __box(ValueException(1)) ;
case 2: throw __box(AnotherValueException(1)) ;
case 3: throw new ReferenceException() ;
case 4: throw 3.1415 ;
case 5: throw __nogc new AnotherValueException(1) ;
case 6: throw new AnotherReferenceException() ;
case 7: throw new int ;
case 8: throw new Int32 ;
default: return ;
}
}
catch ( ValueException __box * )
{
Console::WriteLine( S"ValueException __box *" ) ;
}
catch ( ValueType * )
{
Console::WriteLine( S"ValueType *" ) ;
}
catch ( ReferenceException * )
{
Console::WriteLine( S"ReferenceException *" ) ;
}
catch ( System::Runtime::InteropServices::SEHException * e )
{
Console::WriteLine( S"System::Runtime::InteropServices::SEHException *\n{0}", e->ToString() ) ;
}
catch ( Object * o )
{
Console::WriteLine( S"Object *\nType is {0}", o->GetType()->Name ) ;
}
catch( ... )
{
Console::WriteLine( S"..." ) ;
}
__finally
{
Console::WriteLine( S"__finally" ) ;
}
}
}