Hello, i am italian, I apologize for my English.
I would like to extend the class std::exception:
When I run this piece of code the program goes into 'core', Can you help me understand what's the problem?
Thank you
I would like to extend the class std::exception:
Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>
#include <stdarg.h>
#include <list>
#include <string>
#include <vector>
#include <exception>
using namespace std;
class Exception : public std::exception
{
public:
Exception(const std::exception& e);
Exception(const Exception& e);
Exception(const std::string& s);
Exception& operator= (const Exception& rhs);
void swap(Exception& x);
virtual ~Exception() throw();
public:
virtual const string getMessage() const;
/**
* Returns getMessage()
*/
virtual const char* what() const throw();
private:
string m_msg;
};
Exception::Exception(const std::string& s)
{
m_msg = s;
}
Exception::Exception(const std::exception& e)
: std::exception(e)
{
}
Exception::~Exception() throw()
{
}
Exception& Exception::operator=(const Exception& rhs)
{
Exception(rhs).swap(*this);
return *this;
}
void Exception::swap(Exception& rhs)
{
std::swap(static_cast<std::exception&>(*this), static_cast<std::exception&>(rhs));
std::swap(m_msg, rhs.m_msg);
}
const string Exception::getMessage() const
{
return m_msg;
}
const char* Exception::what() const throw()
{
return getMessage().c_str();
}
int main()
{
string s("22");
try
{
s.erase(10,10);
}
catch (Exception &tExc)
{
cout << tExc.getMessage() << endl;
}
return 0;
}
Thank you
Comment