ltcmelo
February 14th, 2006, 09:03 AM
Hi!
I'm having a conceptual doubt about this issue.
I building a hierarchy of exceptions for my application. So, I need (not that I need, but I want) to inherit my base exception type from exception. This way, others can catch my exceptions in a catch(std::exception e) block. However, I don't know if I should inherit from runtime_error, which already inherits from exception.
The interesting thing is that exception doesn't have a constructor with const char*messge argument. And runtime_error does. BUT, exception actually has the virtual const char* what() {return->message;}. So, it's possible to inherit from exception (just do not make a constructor with this argument) and "hide" the virtal const char* what() method from exception.
So, can any expert help me out clarify this.
Thank you very much.
This compiles (comeau online).
#include <stdexcept>
class MyException : public std::runtime_error {
public:
MyException(const char* m) : std::runtime_error(m) { }
};
int main(int argc, char* argv[])
{
int a = 2;
if (a == 2)
{
throw MyException("teste");
}
return 0;
}
This fails (comeau online)
#include <stdexcept>
class MyException : public std::exception {
public:
MyException(const char* m) : std::exception(m) { }
};
int main(int argc, char* argv[])
{
int a = 2;
if (a == 2)
{
throw MyException("teste");
}
return 0;
}
This also compiles but i get warning (comeau online)
#include <stdexcept>
class GraphManipulationException : public std::exception
{
private:
const char* message;
public:
GraphManipulationException(){}
GraphManipulationException(const char* message):message(message){}
virtual int ha(){return 1;}
const char* what() { return this->message; }
};
int main(int argc, char* argv[])
{
throw GraphManipulationException("test");
return 0;
}
I'm having a conceptual doubt about this issue.
I building a hierarchy of exceptions for my application. So, I need (not that I need, but I want) to inherit my base exception type from exception. This way, others can catch my exceptions in a catch(std::exception e) block. However, I don't know if I should inherit from runtime_error, which already inherits from exception.
The interesting thing is that exception doesn't have a constructor with const char*messge argument. And runtime_error does. BUT, exception actually has the virtual const char* what() {return->message;}. So, it's possible to inherit from exception (just do not make a constructor with this argument) and "hide" the virtal const char* what() method from exception.
So, can any expert help me out clarify this.
Thank you very much.
This compiles (comeau online).
#include <stdexcept>
class MyException : public std::runtime_error {
public:
MyException(const char* m) : std::runtime_error(m) { }
};
int main(int argc, char* argv[])
{
int a = 2;
if (a == 2)
{
throw MyException("teste");
}
return 0;
}
This fails (comeau online)
#include <stdexcept>
class MyException : public std::exception {
public:
MyException(const char* m) : std::exception(m) { }
};
int main(int argc, char* argv[])
{
int a = 2;
if (a == 2)
{
throw MyException("teste");
}
return 0;
}
This also compiles but i get warning (comeau online)
#include <stdexcept>
class GraphManipulationException : public std::exception
{
private:
const char* message;
public:
GraphManipulationException(){}
GraphManipulationException(const char* message):message(message){}
virtual int ha(){return 1;}
const char* what() { return this->message; }
};
int main(int argc, char* argv[])
{
throw GraphManipulationException("test");
return 0;
}