exception question!

why is "mine caught", when b is thrown? isn't it supposed to catch "yours"?
can someone explain plz!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
    #include <string>
    #include <exception>
    using namespace std;
    class a
    {
    public: virtual string whose()
            { return "mine"; }
    };
    class b
    {
    public: virtual string whose()
            { return "yours"; }
    };
    int main () {
      a b;
      try  { throw b; }
      catch (a& e)  { cout << e.whose() << endl; }
      return 0;
    }
This is because the "b" variable is an instance of class "a". If you notice where it was declared a b;, "a" is the type and "b" is the name. This is a great example of why one should use good names for everything. Situations such as these can become confusing.
Topic archived. No new replies allowed.