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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Can constructor throw exception?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I came across this question:
which of the following statements are true about constructor?
1. a class inherits its constructor from its parent class
2. a constructor may have a return type
3. a constructor may throw exception
4. compiler will assign a default constructor if a class does not explicitly declare one.
I chose 4 only, but the answer is 3 and 4. but when I read Khalid books, it clearly say constructor can not throw exceptions. Do I have a real bad memory?
Second question about constructor, I know it has been discussed before, but I want to clarify my confusion.
Because some books say, constructor can be defined final, but Khalid book again claims that there is no other modifiers permitted except accessibility modifier preceding constructor.
Any body step out to shed some light on this?
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi i dont know about hte modifiers but i am pretty sure i reda somewhere that a constructer can throw an exception and it makes sense since you can do everything in a constructor there will be exceptions you will have to throw
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Constructors can throw exceptions.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Jason,
For your question 'can constructors be declared final?', this should clear your doubts.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Constructors can definitely throw exceptions.....
As for them being final, the RHE and Exam Cram books clearly mention that they cannot be final, static, volatile or transient. At the same time they can be private, protected or public.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I agree with Shafeeq Sheikh that Constructors can definitely throw exceptions. One precaution there...
Per Bruce Eckel (TIJ, P-392) -
If your class is a derived class then its constructor must declare any base-class constructor exceptions in its exception specification.
Because a base-class constructor will always be called one way or another.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
...and you can't put it inside a try loop, though you might want to.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Except 1, I think all of them are true.
What do you think Meeelaan?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Yes its the best description
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
    Bookmark Topic Watch Topic
  • New Topic