Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

C++ exception handling wrt class hierarchy

I'm trying to figure out how exception handling works in C++.

Example:
class A {};
class B : A {};
class C : A {};

void f()
{
  try
  {
    throw C();
  }
  catch (B& b)
  {
  }
  catch (A& a)
  {
  }
}

I had the impression that in this example, the exception would be caught in 'catch (A& a)', since A is a base class to C. It doesn't. I'vr tried both in MS Visual C++ and on HP C++ (on a UNIX machine). Both programs behave bad. Do any one out there know how the exception handling should be handled in this case?

Thanks.
0
OlaOdling
Asked:
OlaOdling
  • 2
1 Solution
 
jkrCommented:
Make it read

class A {};
class B : public A {};
class C : public A {};

and it will work :o)
0
 
AlexFMCommented:
class B : public A {};
class C : public A {};

Now it works.

0
 
AlexFMCommented:
Sorry, jkr, you was first.
0
 
OlaOdlingAuthor Commented:
I'm really ashamed!

I have to talk to my boss! I do too little programming
nowadays and too much report writing. I have to get back to the real business, i.e. programming!

Well, easy earned points. Sorry AlexFM, I let the points go to jkr since he posted his answer 4 minutes before you.

Thanks,
Ola
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now