?
Solved

C++ exception handling wrt class hierarchy

Posted on 2003-02-26
4
Medium Priority
?
209 Views
Last Modified: 2010-04-01
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
Comment
Question by:OlaOdling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 800 total points
ID: 8026022
Make it read

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

and it will work :o)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8026052
class B : public A {};
class C : public A {};

Now it works.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8026061
Sorry, jkr, you was first.
0
 

Author Comment

by:OlaOdling
ID: 8026123
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question