| Author |
Constructors and Exceptions?
|
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
|
Can constructors throw Exceptions?? Why is it said that the no-arg constructor of the superclass, must not throw an exception??
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
Yes, of course they can throw exceptions. Where did you get that from about not allowing exceptions from no-arg constructors? I couldn't find it in the Java Language Specification.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Also, any superclass constructor can throw an exception. The only limitation is, the subclass constructor can't catch that exception so it must throw it as well.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
|
If the subclass has a default constructor and it calls the super constructor which throws an exception, then it will lead to compiler error, right???
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
You don't write a default constructor, so you can't catch it, so it would count as an unhandled exception and (if checked) would cause a compiler error.
|
 |
 |
|
|
subject: Constructors and Exceptions?
|
|
|