exception question

I do not understand why the output of my code is :
Logic error: 0
Logic error: 1
Exception: Unknown exception
Exception: 2
Something bad happened

What I understand is when i=0, it corresponds to case 0, which is then caught by catch(...) and is supposed to print "Something bad happened".
why is it that when i=0, the compiler outputs Logic error: 0 ?
isn't catch(exception ex) supposed to catch only the case2?

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
#include <iostream>
#include <exception> 
#include <stdexcept>
using namespace std;
void function(int i) {
	switch(i) {
	case 0: throw domain_error("0");
	case 1: throw logic_error("1");
	case 2: throw exception();
	case 3: throw range_error("2");
	case 4: throw "so bad";
	}
}
int main(void) {
	for(int i = 0; i < 5; i++) {
		try {
			function(i);
		}
		catch(logic_error le) {
			cout << "Logic error: " << le.what() << endl;
		}
		catch(exception ex) {
			cout << "Exception: " << ex.what() << endl;
		}
		catch(...) {
			cout << "Something bad happened" << endl;
		}
	}
	return 0;
}
domain_error inherits logic_error, meaning it can be implicitly downcasted.
http://www.cplusplus.com/reference/stdexcept/domain_error/

-Albatross
oh I understand, I read about that in the previous chapter of my book but never really paid attention. Thanks!
Topic archived. No new replies allowed.