Advanced Exception Handling

Your homework assignment is to implement the flException classes and macros, as discussed in class. As with previous classes, replace fl with your initials.

Your goal is to replace all error handling and exceptions in any of your classes to now use the new MGException approach. You will need to do the following:

Modify Test Program

Modify your test program to handle the MGException class only, for errors. Your test program for main will simply be the following:

	MGString X;
	X = "A Test";
	try
	{
		X[10] = 'A';
	}
	catch( MGException& E )
	{
		cout << "Error: " << E << endl;
	}

Add an operator[] to MGString

Add the operator[] function to your MGString class:

char& MGString::operator[](int Index );

This function returns the character at m_Str[Index]. If Index is an invalid index, then you should throw an MGStringException object.

Define the MGException base class

The MGException base class should be defined as derived from the MGObject class, and have the following functions:

	MGException( const char* Message=0 )
	virtual bool GetAsString( char* Dest ) const;
	virtual bool SetFromString( const char* Src );
	virtual bool SaveToFile( ofstream& Dest ) const;
	virtual bool LoadFromFile( ifstream& Src );

These functions should perform the same type of task for the MGException object as they have in other points in the framework.

Note: In class we discussed a technique where you would not have to write SaveToFile and LoadToFile here. You are free to implement that technique.

You will need to add a data member to your MGException class to store the message string. The following is recommended:

char m_Str[512];

Define the derived classes

You must define the following exception classes, derived from MGException. Remember the purpose of these classes is really only to provide a constructor that will initialize the m_Str of the base MGException class with an appropriate error message.

MGFileException

The constructor for this class should be written as:

MGFileException( const char* Operation, const char* File, const char* SrcFile, int Line )

MGMemoryException

The constructor for this class should be written as:

MGMemoryException( const char* Operation, int Size, const char* SrcFile, int Line  )

MGStringException

The constructor for this class should be written as:

MGStringException( const char* Operation, const char* SrcFile, int Line  )

For each of the classes above, a simplified macro should be written to help the user throw the exception. Each class has 1 macro. Remember to make it a macro, with #define, and not a function. The macros are:

#define MGThrowFileException(Op,F) ...
#define MGThrowMemoryException(Op,S) ...
#define MGThrowStringException(Op) ...

Use the __FILE__ and __LINE__ in your macro, as the last two parameters to the actual constructors.

Write the operator<< function

As discussed in class, you must write the operator<< function for the MGObject base class, using the GetAsString function from within the operatot<< function. The prototype for this non-member function is:

ostream& operator<<( ostream& Dest, const MGObject& Src );