• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Constructors that throws Exception.

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While doing Dan's mock exam I came across this question:


class A {
A() throws Exception {} // 1
}
class B extends A {
B() throws Exception {} // 2
}
class C extends A {} // 3

Which of the following statements are true?
a. Compiler error at 1.
b. Compiler error at 2.
c. Compiler error at 3.
d. None of the Above
ANS: c



I answered d thinking that since an overriding method does not need to declare any throws clause(even if the overridden method has one), then it must be the same for constructors.
I understand that constructors are not like methods, and the JLS did specified that


JLS 8.8.7
A default constructor has no throws clause.
It follows that if the nullary constructor of the superclass has a throws clause, then a compile-time error will occur.


But so far, I don't see any reason why the rules have to be any different for constructors? Why does the rule "may not be declared to throw more checked exceptions" not applicable to constructors?
Thanks.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, let me try answering my own question.
Constructors are called in successions i.e. the subclass will call its superclass' constructors, and so forth. Now if the call to the constructor is implicit, obviously you cannot put it inside a try/catch clause. And if the call is explicit, you also cannot put it inside the try/catch clause because of the provision that the first statement must either be super() or this(). So now if the superclass constructor throws an exception, the subclass constructor has no choice but to throw it also. So that is why it has to declare it.
Am I right on this one?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton. That is the same reasoning process I followed when I tried to explain it to myself. I looked for confirmation in the JLS and other places and I could never find any.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your description was the first thing that popped in my mind and seems fully logical. After all, you have to handle or declare and when you're getting to class C, you're neither handling or declaring. Sounds like an explosion waiting to happen...
Ross
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found something!

If an exception is thrown during the construction process, the new expression terminates by throwing that exception--no reference to the new object is returned. As an explicit constructor invocation must be the first statement in a constructor body, it is impossible to catch an exception thrown by another constructor.
(If you were allowed to catch such exceptions it would be possible to construct objects with invalid initial states.)


In my favorite Java book, The Java Programming Language, 3.2.1 Constructor Order Dependencies
[ August 05, 2003: Message edited by: Marlene Miller ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic