File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instance block 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 "instance block" Watch "instance block" New topic

instance block

payal sharma
Ranch Hand

Joined: Oct 04, 2000
Posts: 57
If an instance initializer block does not catch a checked exception that can occur during its execution ,then the exception must be declared in the throw clause of every constructor in the class.
One the other hand it is also written that constructor can not specify exception in the header.
please explain
thanks in advance
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Payal,
Your second statement is wrong (your other hand is incorrect)!
To prove it wrong we can just look at the* package and see many constructors with exceptions specified. For example, let's look at FileInputStream:
public FileInputStream(File file)
throws FileNotFoundException
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
That is correct - If the instance initializer is not catching a Checked Exception the Constructor should take care of it, by declaring in the throws clause.
The following code snipplet may be useful

Hope this helps,
SCJP2,OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
I agree. Here's the link:
subject: instance block
It's not a secret anymore!