not understanding try{}, catch(){} in void funtion

This program is part of calculator discussed in B.Stroutrup's "principles and practice using C++" book. We (actually he) made seperate function calculate() and put it into main so after receiving error we shouldnt have to quit program but instead we just get an error message and we can continue.
That sounds good but when we get an error inside calculate's() try{} we are thrown into catch(){} part of this calculate() function (right?). After we clean_up_mess() arent we supposed to be at the end of this calculate() function and than just exit it and return to main()? (Or void funtion without return; is supposed to be like infinite loop?)
I dont think so because
1
2
3
void print(){
cout << "Hello World!" << endl;
}

is working great and i dont get full screen of Hello World's.
As i understand void is not supposed to give any return value so it just executes and goes back to main(), but in this case i can continue to type expressions and get solid answers.
Could someone explain to me whats going on?

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
const char print = ';';
const string prompt = "> ";
const string result = "= ";

void clean_up_mess()
{
	ts.ignore(print);  // ignore all the cin inputs till print
}

void calculate()
{
	while (true) try {
		cout << prompt;
		Token t = ts.get();
		while (t.kind == print) t = ts.get();
		if (t.kind == quit) return;
		ts.unget(t);
		cout << result << statement() << endl;
	}
	catch (runtime_error& e) {							
		cerr << e.what() << endl;						
		clean_up_mess();								
	}													
}		

int main()																														
try {		
	calculate();													
	return 0;
}
catch (exception& e) {
	cerr << "exception: " << e.what() << endl;
	char c;
	while (cin >> c&& c != ';');
	return 1;
}
catch (...) {
	cerr << "exception\n";
	char c;
	while (cin >> c && c != ';');
	return 2;
}

On line you have while (true) Statement. The Statement, a one-liner, starts with word "try" and ends with character '}' (on line 23).

There are only two ways to break out from that while loop:
1. The return on line 16
2. An exception that manages to slip through the catch unhandled.
Ohh :) I didnt see that while(true) was outside of try{} Thanks for opening my eyes :)
Topic archived. No new replies allowed.