need help with exceptions

This is a discussion on need help with exceptions within the C++ Programming forums, part of the General Programming Boards category; I have defined a class called Exception as follows: #include <iostream> #include <string> class Exception{ string s; public: Exception(string error): ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    need help with exceptions

    I have defined a class called Exception as follows:

    #include <iostream>
    #include <string>
    class Exception{
    string s;
    public:
    Exception(string error): s(error){};
    friend ostream& operator << (ostream& os, Exception& e){
    return os << "Exception: " << e.s;
    }
    };

    I want a method in another class to throw an Exception. The Class is called PolyLine and it defined in PolyLine.h. What do I have to put in my method in PolyLine.h in order for this to work?

    to I have to state that the method throws and excemption? if so where do I do it and what is the syntax?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    The code below shows how to use try, throw and catch (exception handling). It fairly crude but it does show one way it can be used.

    Code:
    #include "stdafx.h"
    #include <iostream> 
    #include <string> 
    using namespace std;
    
    class Exception{ 
    
    	string s; 
    
    public: 
    	
    	Exception(string error): s(error){}; 
    	
    	friend ostream& operator << (ostream& os, Exception& e)
    	{ 
    		return os << "Exception: " << e.s; 
    	} 
    }; 
    
    class CThing
    {
    	int m_thing;
    public:
    	CThing( int n = -1 ) : m_thing( n )
    	{ }
    	int GetThing( )
    	{
    		return m_thing;
    	}
    
    	void SetThing( int n )
    	{
    		m_thing = n;
    	}
    
    };
    
    string e( "Thing initialize to -1" );
    
    Exception ex( e );
    
    bool GetSomething( CThing & thing );
    
    int main(int argc, char* argv[])
    {
    	bool Cont = true;
    	CThing tg;
    	
    	while ( Cont )
    	{
    		try
    		{
    			Cont = GetSomething( tg );
    		}
    		catch ( Exception &exc )
    		{
    			cout << exc;
    			
    			//Correct problem and continue
    			tg.SetThing( 0 );
    		}
    		catch ( ... ) //if you remove the above catch it will use the catch all catch here
    		{
    			cout << "Catch anything and avoid the nasty system Exception message" << endl;
    		}
    	}
    
    	return 0;
    }
    
    
    bool GetSomething( CThing & thing )
    {
    	if( -1 == thing.GetThing( ) )
    	{
    		throw ex;
    	}
    
    	cout << "\n\nDid something" << endl << endl;
    
    	return false;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  2. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 11:20 AM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21