Exception error

Am trying to catch exception error in base class costructor and its add function it show result but values are wrong here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 class Distance
{

private:
	float inches;
	int feet;
public:
	class A
	{
	public:
	string message;
		float in;

		A():message("none"),in(0){}
		A(string m , float i){}
	};
	class B
	{
	
	public:
		string mess;
		float inc;

		B():mess("none"),inc(0){}
		B(string m , float i){}
	};
	
	 Distance():inches(0),feet(0){}
	 Distance(float n, int f){
	if(n>=12)
		throw A("Constructor Distance",n);
		inches=n;
		feet=f;
}
	void add(float n,int f)
	{
		if(n>=12)
			throw A("add",n);
		inches=n;
		feet=f;
	}
	void Display()
	{
		cout<<"Inches :" <<inches<<endl<<"Feet :"<<feet<<endl;
	}
};
int main()
{
	
	try
	{
		Distance d(15,15);
		Distance d1;
		d1.add(15,15);
	}
	catch(Distance::A a)
	{
		cout<<"Intialization error\n";
		cout<<"Problem in function"<<a.message<<endl;
		cout<<"Value is "<<a.in<<endl;
		
	}
	catch(Distance::B b)
	{
		cout<<"Intialization error\n";
		cout<<"Problem in function"<<b.mess<<endl;
		cout<<"Value is "<<b.inc<<endl;
		
	}
	cout<<"This step will run now \n";


	getch();

}
The overloaded constructors of A and B do nothing:

A(string m , float i){} // should set message and in, but you don't

B(string m , float i){}// should set mess and inc, but you don't

Hope that helps.
Last edited on
OFcourse how can i forget that after doing the changes it does give output even if i delete line 63 to 69 why is that
You never throw a B, so there are no B s to be caught therefore that catch block will not be executed.
Thank you but after changing i still get output as
1
2
3
Problem in functionConstructor Distance
Value is 13//i changed constructor value for observing the change
This step will run now

and i want output of
1
2
3
4
5
6
Problem in functionConstructor
Value is 13

Problem in functionDistance
Value is 15
This step will run now
Last edited on
Hi, sorry for the late reply.

I think you may want to re-read the section on exception handling in your text book (or whatever you are learning from).

In the try block, only one exception will be thrown. In your example, it is thrown when you call the constructor for Distance on line 52. When that exception is thrown, lines 53 and 54 don't get executed therefore the exception in function add never gets thrown (because the call to add was never made). We could say (and do not rely on this description as I am unsure of how accurate it is) that the flow of execution jumps from line 52 when the exception is thrown (technically the exception is thrown on line 31, but that isn't overly relevant) to line 56 where the first matching catch block is.

Once the exception is caught, it can be handled or ignored. After that the program will continue executing instructions that appear AFTER the catch black (meaning that it won't try to resume executing from where the exception was thrown), or, if we put it in a loop, it will continue with the loop.

Hopefully that will help clear things up a little bit. Be sure to read whatever material you have (text book, lecture slides etc...) on exceptions as well (there's also a pretty awesome tutorial on this site: http://www.cplusplus.com/doc/tutorial/exceptions/ )
Thanks :)
Topic archived. No new replies allowed.