• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Exception handling and inheritance

 
Ranch Foreman
Posts: 43
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

When doing one of the OCA mock exams, I came across the following: What is the output of the following code?



In my mind, I was hesitating between two different ways of approaching this.

The first one being: an E2 instance is also an E1 instance, which will be caught by the first catch block and then the code will jump to the finally block.

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?

Any help is much appreciated (as always)!
Brecht


 
Saloon Keeper
Posts: 10308
217
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exception is always handled by the first catch clause that fits. An E2 is both an E1 and an Exception, so the catch clause for E1 will be executed, because it appears first. If you swap lines 8 and 9 with lines 10 and 11, the catch clause for Exception would have been executed.
 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While your understanding sort of correct, the trick probably over here is to understand which catch blocks catch an exception. And the answer is - the very first which can.

Can an E1 exception type catch E2? Yes, it can, because E2 IS-A E1.

Can technically E1 not be caught and then proceed with the next catch? Yes, it can, if the exception would be thrown other than E1 or E2. So to have a wider type as Exception makes sense. But what would happen if you'd swap catch blocs E1 with Exception? Would that make sense? Could be the scenario that Exception wouldn't be caught, but E1 would?
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If you swap lines 8 and 9 with lines 10 and 11, the catch clause for Exception would have been executed.


Well, that's not true. In such case, E1 technically couldn't be reached in any case.
 
Stephan van Hulst
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to recall it was possible at some time. I'm probably mistaken.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Brecht Geeraerts
Ranch Foreman
Posts: 43
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, everyone, for taking the time to answer and help me out!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!