C++ Acessing Virtual Functions of Custom Exception Class from Catch Statement
I am now working on an idea to create a single catch statement and identify the validation and some run time exceptions. So I did create two custom exception classes with the virtual function for what(). The problem is that I am getting std::exception when I tried to access the virtual function.
Whenever there is an exception that occurs, I created a pointer object for the base class and an object for the custom execption class and i assigned the reference of the custom exception class object to the base class pointer and I did throw the base class pointer and tried to access it from the catch statement but I am getting std::exception as output! Help me out where I am wrong.
This is my Header File
HTML Code:
#include<iostream>
#include<exception>
using namespace std;
class A
{
protected:
int a,b;
public:
bool setdata();
virtual void calculate()=0;
};
class B : public A
{
virtual void calculate();
};
class DivideException : public exception{
public:
virtual const char * what() const throw()
{
return "It is a divide by zero exception\n";
}
};
class InputException : public exception{
public:
virtual const char * what() const throw()
{
return "It is an Input Exception";
}
};
This is my Implementation .cpp File
HTML Code:
#include"head.h"
#include<string>
#include"sstream"
void B :: calculate()
{
try{
cin>>a;
if(!cin)
{
exception *i; //The Way I tried to access the Virtual Function
InputException f;
i=&f;
throw i;
}
cin>>b;
if(!cin)
{
exception *i;
InputException f;
i=&f;
throw i;
}
if (b == 0)
{
exception *j;
DivideException d;
j=&d;
throw j;
}
else
{
cout << "a / b = " << a/b << endl;
}
}
catch( std::exception* e)
{
cout<<(e->what()); //No matter what Exception I throw, it keeps printing me std::exception
}
}
Here is my main.cpp file, it is quite simple.
HTML Code:
#include"head.h"
int main()
{
A *obj;
B e;
obj=&e;
obj->calculate();
return 0;
}
What am I doing wrong here????? Sample I/O:
1 0 std::exception
1 a std::exception
Output I am expecting: 1 0 It is a Divide by Zero Exception.
1 a It is a Input Exception
Re: C++ Acessing Virtual Functions of Custom Exception Class from Catch Statement
typically, you throw by value and catch by (const)reference:
Code:
void B::calculate()
{
try
{
if( cin >> a && cin >> b )
{
if( b != 0 )
{
cout << "a / b = " << a/b << endl;
}
else
throw DivideException();
}
else
throw InputException();
}
catch( std::exception const& err )
{
cout << err.what() << std::endl;
}
}
why are you throwing pointers ( to zombie objects ) ?
FYI, there's also std::runtime_error/etc... exposing constructors to properly initialize the error message ( no need to ovverride "what()" ); note that exceptions should form a meaningful hierarchy ( typically via virtual inheritance ) in order to be useful ...
Re: C++ Acessing Virtual Functions of Custom Exception Class from Catch Statement
Thank you so much superbonzo.
Let me tell you what I thought process I had.( I am a newbie, so please don't mind if it is silly)
I learnt that while using pure virtual functions, I will have to create a base class pointer and add reference of the object of the derived class to access the virtual member function of the derived class.
So, if any exception occurs, Let's say InputOutputException
exception *i; //Created an pointer to the base class
InputException f; // Did create an object to the derived class
i=&f; // Adding the reference of the derived class to the base class pointer
throw i; //throwing the pointer!!
Now since I am throwing an pointer of the base class, I created another pointer in the catch,, catch(exception *e) here..
so I thought the pointer of the exception class i will assign to the e , the another pointer i have in exception class.. so I thought I this new pointer will have all access to the member functions same as the old pointer will have..
So e->what() would return me that message is what I thought!
Thanks in advance! And, your piece of code did work! :) :) Tyyy! :)
Re: C++ Acessing Virtual Functions of Custom Exception Class from Catch Statement
uhm, there's a lot of confusion in there ...
consider this code
Code:
struct A{ void foo(){} };
int main()
{
A* p;
{
A a;
p = &a;
}
p->foo();
}
is the code above legal ? what do you think will happen ? why ?
you should cover the basics before starting to play with exceptions. Are you following some introductory book ? if not, I'm sure someone here will be more than happy to suggest you a good one ...