Catch Exception : try catch « Statement « Visual C++ .NET






Catch Exception

 

#include "stdafx.h"
using namespace System;

ref class X {};
ref class Y {};

void main()
{
    X ^x = gcnew X;

    try
    {
         Y ^y = (Y^)x;   
         Console::WriteLine("No Exception"); 
    }
    catch (InvalidCastException ^e)
    {
        Console::WriteLine("Invalid Cast Exception");
        Console::WriteLine(e->StackTrace);
    }
} 

   
  








Related examples in the same category

1.Catch an Exception with a try/catch Block
2.Catch All exceptions
3.Multi Exception Handling
4.Catch IndexOutOfRangeException
5.Catch IO exception
6.Nested exception catching
7.Catch custom exception and then general exception