Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Constructors and Exceptions

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic