File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 519

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors and Exceptions
 
Similar Threads
Can constructors throw Exceptions?
why is it wrong?
Exceptions (long one) Your response requested.
question about anonymous class
Constructor vs instance initializer