Link to home
Start Free TrialLog in
Avatar of OlaOdling
OlaOdling

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

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

Now it works.

Sorry, jkr, you was first.
Avatar of OlaOdling

ASKER

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