Hello guys and gals. This is my first topic, so don't mock me X-D.
I'm relatively new to C++, but I've got some notions on programming from other languagens (Object Pascal/Delphi, Java).
So, here is what I'm trying do do:
I've got two exception classes, called CmyException and CmyFileException. The last one inherits from CmyException, and this one from stl::exceptino. Ok, so far so good. The problem lies in the virtual method "checkMessage()".
In the following code:
#include <string>
#include <iostream>
#include "myFileException.h"
using std::string;
using std::cout;
using std::endl;
int main( int argc, char *args[] )
{
try
{
throw CmyFileException( CmyFileException::excNoLoadedFile );
}
catch( exception &e )
{
cout << e.what() << endl;
}
}
when I call "e.what()", It actually gets the CmyException string ("Default Message"), instead of the CmyFileException, from the virtual method "checkMessage()". Why does this happen? Shouldn't it try to get checkMessage from CmyFileException before CmyException? BTW: I tryed without the virtual method, and I've had the same problem...
Here are the other files:
myException.h
#ifndef INC_MYEXCEPTION_H
#define INC_MYEXCEPTION_H
#include <exception>
using std::exception;
class CmyException : public exception
{
public:
typedef enum typeExcepts
{
excUnknownError = -1,
excNotImplemented = -2,
excCouldNotInitialize = -3
};
//Constructor/Destructor
CmyException( int option ) throw();
~CmyException( void ) throw();
virtual const char* what( void ) const throw();
virtual const int code( void ) const throw();
protected:
int id;
char* message;
virtual void checkMessage( void ) throw();
private:
};
#endif //INC_MYEXCEPTION_H
myException.cpp
#include "myException.h"
CmyException::CmyException( int option ) throw()
{
this->id = option;
this->checkMessage();
}
CmyException::~CmyException( void ) throw()
{
/* ... */
}
void CmyException::checkMessage( void ) throw()
{
switch( this->id )
{
case excUnknownError :
this->message = "Unknown error!";
break;
case excNotImplemented :
this->message = "Not implemented yet!";
break;
case excCouldNotInitialize :
this->message = "Could not initialize object";
break;
default :
this->message = "Default Message!";
break;
}
}
const char *CmyException::what(void) const throw()
{
return this->message;
}
const int CmyException::code( void ) const throw()
{
return this->id;
}
myFileException.h
#ifndef INC_MYFILEEXCEPTION_H
#define INC_MYFILEEXCEPTION_H
#include "myException.h"
class CmyFileException : public CmyException
{
public:
typedef enum typeExcepts
{
excNoLoadedFile = 0
};
CmyFileException( int option ) throw();
~CmyFileException( void ) throw();
protected:
virtual void checkMessage( void ) throw();
private:
};
#endif //INC_MYFILEEXCEPTION_H
myFileException.cpp
#include "myFileException.h"
#include "myException.h"
CmyFileException::CmyFileException( int option ) throw() :
CmyException( option )
{
/* ... */
}
CmyFileException::~CmyFileException( void ) throw()
{
/* ... */
}
void CmyFileException::checkMessage( void ) throw()
{
switch( this->id )
{
case excNoLoadedFile :
this->message = "Could not load the file";
break;
default :
CmyException::checkMessage(); //Don't know if this is correct...
//I'm trying to do something like a "inherited;" command from Delphi here...
break;
}
}
Oh, and since i'm asking about c++, in this piece of code:
CmyFileException::CmyFileException( int option ) throw() :
CmyException( option )
{
/* ... */
}
Is there another way to call the CmyException contructor? I mean, a different sintax, or something? (Just for curiosity)
Thansks in advance :D