I was reading about custom exception.. This is what I did.
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
class myBase : public exception
{
public:
myBase() {};
virtual ~myBase(void) {};
virtual const char* what() const { return "Hello, you have done a mistake"; }
};
class myDerived : public myBase
{
public:
myDerived() {};
virtual ~myDerived(void) {};
const char* what() const { return "Hello, you have done a mistake in - derived class"; }
};
int main(void)
{
try
{
throw myDerived();
}
catch (exception& e)
{
cout << e.what() << endl;
}
getchar();
return 0;
}
I understood that, I can throw my custom class objects and can catch it. I don't understand the purpose of this. Can someone explain me why do we need custom exception class? Any practical examples will help me understanding the purpose of using custom exception classes.
Thanks.