Question

So is it a good idea to create Exception as inner class? I have a feeling that something wrong, but I do not know what exactly..

For example:

class A {

  ...something...

public:
  class AException : std::exception {
    ...something...
  }
};
Was it helpful?

Solution

It depends.

A nested class is just that, a nested class. This isn't , so there's nothing magical about inner classes.

You can use the "outer class" as the namespace. If this enhances readability, there's something to be said in favour of it.

One disadvantage is that now the exception class is part of the public interface of the class, which

  • prevents you from declaring the exception class in a separate header
  • might hinder maintenance of the containing class (ODR rule)

All else being equal, I'd probably advise against nesting the exception types, since with good naming the benefits should not outweigh the potential drawbacks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top