Consider the following code:
It simply takes 2 class objects(int,float) as feet,inches...
If value of inches exceeds 12 then it throws an exception and also specifies the name of the function where this exception occurs by defining arguments in the exception class which is thrown
#include<iostream>
#include<string>
using namespace std;
#include<conio.h>
////////////////////////////////////////////////////////////////////////////////
class Distance
{
private:
int feet;
float inches;
public:
class InchesEx
{
public:
string origin;
float iValue;
InchesEx(string ori , float in)
{
origin=ori;
iValue=in;
}
cout<<"\n\ndist1 is given as "; dist1.showdist();
cout<<"\n\ndist2 is given as "; dist2.showdist();
}
catch(Distance::InchesEx ix)
{
cout<<"\nInitialisation error in "<<ix.origin
<<" .\n Inches value of "<<ix.iValue
<<" is too large.......";
cout<<"\n\nABORTING!!!!!!";
}
cout<<endl;
getch();
return(0);
}
dis workes perfectly fine...
Now if we make just a small change in the program by replacing "ori" by "or"(in d 2-argument constructor of class InchesEx d program looks like:
#include<iostream>
#include<string>
using namespace std;
#include<conio.h>
////////////////////////////////////////////////////////////////////////////////
class Distance
{
private:
int feet;
float inches;
public:
class InchesEx
{
public:
string origin;
float iValue;
InchesEx(string or , float in)
{
origin=or;
iValue=in;
}
cout<<"\n\ndist1 is given as "; dist1.showdist();
cout<<"\n\ndist2 is given as "; dist2.showdist();
}
catch(Distance::InchesEx ix)
{
cout<<"\nInitialisation error in "<<ix.origin
<<" .\n Inches value of "<<ix.iValue
<<" is too large.......";
cout<<"\n\nABORTING!!!!!!";
}
cout<<endl;
getch();
return(0);
} But now, d following errors are reported by d compiler:
17 expected `,' or `...' before '||' token
In constructor `Distance::InchesEx::InchesEx(std::string)':
19 expected primary-expression before '||' token
19 expected primary-expression before ';' token
20 `in' was not declared in this scope
In constructor `Distance::Distance(int, float)':
29 no matching function for call to `Distance::InchesEx::InchesEx(const char[23], float&)'
note:13 candidates are: Distance::InchesEx::InchesEx(const Distance::InchesEx&)
Distance::InchesEx::InchesEx(std::string)
In member function `void Distance::getdist()':
38 no matching function for call to `Distance::InchesEx::InchesEx(const char[19], float&)'
note E:\Anurag Main Docs\bond.cpp:13 candidates are: Distance::InchesEx::InchesEx(const Distance::InchesEx&)
Distance::InchesEx::InchesEx(std::string)