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:True or false

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I just want to confirm whether the following statement is true or false
Constructors can throw exceptions. If parent default constructor throw exception, subclass have to do the same or have to handle the exeption.
Thanks.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Constructors can throw exceptions. If parent default constructor throw exception, subclass have to do the same or have to handle the execption


the above statement is true. the subclass whose parent constructor throws exception ,has to handle the same exception or superclass of the exception thrown by the constructor of the super class.
try the following code
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because a subclass constructor will call the superclass constructor as its first job before executing anything else.

The superclass constructor may throw an exception which in turn may be thrown in the subclass constructor since the latter called the former.
As far as exceptions are concerned, you can see the invocation to super() as a normal method invocation which may not terminate by returning gracefully but by throwing an exception instead. The thrown exception is to be handled by the subclass constructor (try-catch) or thrown ahead...
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to add, that it need not declare exceptions if they are unchecked exceptions.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic