+ Reply to Thread
Results 1 to 2 of 2

Thread: Can anyone guide me using using exception in C++?

  1. #1

    Can anyone guide me using using exception in C++?

    Can anyone guide me using using exception in C++?

    How to use it?

    Please provide some example.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    26
    Below example demonstrates how to create your own class to handle the exception in C++:

    Code:
    class CError
    {
    	char m_szErrorMessage[100] ;
    public:
    	CError(char szErrorMessage[100])
    	{
    		strcpy(m_szErrorMessage, szErrorMessage) ;
    	}
    
    	const char * GetErrorString()
    	{
    		return m_szErrorMessage ;
    	}
    } ;
    
    void ThrowException()
    {
    	throw CError("Hello World to C++ exception") ;
    }
    
    void UseException()
    {
    	try
    	{
    		ThrowException() ;
    	}
    	catch(CError &objError)
    	{
    		const char *p = objError.GetErrorString() ;
    
    		OutputDebugString(p) ;
    	}
    }
    Tips:
    a. Do not use new to throw an exception.
    b. Always catch exception by reference!

    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts