Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / C++/CLI

Exception handling in Managed C++

Rate me:
Please Sign up or sign in to vote.
1.95/5 (12 votes)
1 Aug 20033 min read 88.5K   1.2K   20  
An exception is any error occurring in the program at execution time. Exception handling increases the program reliability.
/*******************************************************************************************


  File Name				:		Exception.cpp
  Platform				:		Visual C++ .NET
  Aut					:		Selvam.R
  Des					:		Exception Handling in Managed C++ Sample



*********************************************************************************************/

#using <mscorlib.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Threading;
using namespace System::IO;
using namespace System::Windows::Forms;


// Display the Exeception Details
void DisplayMessage(String *exType,String *exMessage,String *exStack)
{
	 Console::WriteLine ("Exception Type:\n   {0}", exType);
     Console::WriteLine ("Exception Des:\n   {0}", exMessage);
     Console::WriteLine ("Stack Trace out:\n{0}", exStack);
}

// Divide by Zero Exception
void DivideException()
{

	int i = 0;

	try{
        
		i=300/i;
	
      }catch(Exception *e)
	{DisplayMessage(e->GetType()->ToString(),e->Message,e->StackTrace);} 


}

//Null Object reference Exception
void NullException()
{
      
      Object *obj = (Object *)0;

	  try
	  {
            Console::WriteLine(obj->ToString());
	  }catch(Exception *e)
	{DisplayMessage(e->GetType()->ToString(),e->Message,e->StackTrace);}  
  
     
}

// File Notfound Exception
void FileException()
{

	try
	{
     
		FileStream *fs = new FileStream("data.txt", FileMode::Open);
        StreamReader *sr = new StreamReader(fs);
        
    }
    catch(FileNotFoundException *e)
    {DisplayMessage(e->GetType()->ToString(),e->Message,e->StackTrace);}        

}


// User defined Exception
__gc class MyException:public ApplicationException
{
   public: 
      MyException(String *msg):ApplicationException(msg)
	  {
		  
		  Console::WriteLine(msg);
	  }
};


// Main function in the Exception handling 
int main()
{
	//declare the variable
	char ans;
	Console::WriteLine("\t\t Exception Handling ");
	Console::WriteLine("\t\t ****************** ");
	Console::WriteLine("\t\t 1==>Divide By Zero Exception");
	Console::WriteLine("\t\t 2==>Null Object Exception");
	Console::WriteLine("\t\t 3==>File Exception");
	Console::WriteLine("\t\t 4==>User Defined Exception ");

	// Read from user input
	ans=Console::Read();
	
	switch(ans)
	{// Start switch statement
	case '1':
		DivideException();
		break;
	case '2':
		NullException();
		break;
	case '3':
		FileException();
		break;
	case '4':
		throw new MyException("This is my exception");
		break;
	default:
		Console::WriteLine("Please chose currect Exception [1-4]!");
		
	}// end switch

  return 0;
	
}// end main 


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
India India
Selvam has worked on several technologies like Java, Python, Big data, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoys writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Big Data
o Google Professional Data Engineer 2021
o Confluent Certified Developer for Apache Kafka 2020
o Datastax Apache Cassandra 3.x Developer Associate Certification 2020
✓ Cloud
o Google Professional Cloud Architect 2021
o Microsoft Certified: Azure Solutions Architect Expert 2020
o AWS Certified Solutions Architect - Associate 2020
✓ Oracle Certified Master, Java EE 6 Enterprise Architect (OCMEA) 2018

Github : https://github.com/selvamselvam
Web site: http://www.careerdrill.com
Linkedin: https://www.linkedin.com/in/selvamselvam/

Comments and Discussions