aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors and Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors and Exceptions" Watch "Constructors and Exceptions" New topic
Author

Constructors and Exceptions

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 526

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.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constructors and Exceptions