Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors and Exceptions

 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please confirm my assumption.

If a constructor throws a checked exception (say IOException), then ANY subclass of this class must have an EXPLICIT constructor which throws this exception or a superclass of the exception, correct?

But when dealing with methods, the overriding method must not throw any new or broader checked exception, so it's basically the complete opposite to constructors?

Thanks in advanced.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Marzo]: If a constructor throws a checked exception (say IOException), then ANY subclass of this class must have an EXPLICIT constructor which throws this exception or a superclass of the exception, correct?

Not quite. If there is more than one constructor in the superclass, and at least one constructor does not throw the checked exception, then a subclass constructor may avoid the checked exception as well as long as it invokes the correct super constructor. E.g.

so it's basically the complete opposite to constructors?

Yes, in terms of what checked exceptions may be thrown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic