Brecht Geeraerts wrote:Another way that got in my mind was: when calling the E2 constructor (on line 7), this is a default no-args constructor. This one will call its super no-args constructor (i.e. E1() ) which in turn will also call its super constructor (i.e. Exception() ), both of which are added by the compiler. Therefore the exception that is thrown will be an instance of Exception, which is not caught by the catch block on line 8 but rather by the one on line 10.
I know the second one is not correct. But could someone tell me why this is not correct and how I could have known this in advance?
When you create an object of class C, you call a constructor of class C. This constructor
must call some constructor of C's supertype, and that constructor
must call some constructor of the supertype of that supertype, and all the way until a constructor of class Object is called. This always happens for every class.
So in your example, you create an object of class E2. In this case the no-args constructor is called, and it calls some constructor of E1, and so on, all the way until some constructor of Object is called. Do you really want to say that this object is a instance of Object?
Well,
you should, because it
is an instance of Object. Every object of every class is an instance of Object.
But that's not relevant to all this discussion about exceptions. You create an object of class E2, and you get an object of class E2. All that business about constructors is just a diversion with no meaning.