I have tried a simple C++ CException implementation class, deriving from std::exception. What did I do wrong, what should I add more, what should I improve on? What is it more to c++ exceptions that one should take into consideration? The aim is to make it as much platform independent as possible. The code follows:
Edited:
class CeException: public std::exception {
public:
char* getascii(const wchar_t* msg)
{
char* pasc = new char[wcslen(msg) + 1 ];
wcstombs(pasc, msg, wcslen(msg) + 1);
return pasc;
}
CeException(const wchar_t* msg, char* pasc = NULL ):
exception(pasc = getascii(msg))
{
delete[] pasc;
}
CeException(const string msg)
{
}
virtual ~CeException()
{
}
BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL )
{
const char* pasc = this->what();
wchar_t* puni = new wchar_t[strlen(pasc)+1];
mbstowcs(puni,pasc, strlen(pasc) + 1);
wcscpy_s(lpszError,nMaxError, puni);
delete[] puni;
return 0;
}
void Delete()
{
delete this;
}
};
I have edited with my final implementation, using your indications.
Typically you don’t need to implement methods inside exception class, so it might look something like this:
I don’t understand the reasons to have other methods.
You should not throw exception from it’s constructor. Instead, client code should throw your exception: