Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance block

 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Payal,
Your second statement is wrong (your other hand is incorrect)!
To prove it wrong we can just look at the java.io.* package and see many constructors with exceptions specified. For example, let's look at FileInputStream:
public FileInputStream(File file)
throws FileNotFoundException
Regards,
Manfred.
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Payal,
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,
Sandeep
SCJP2,OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic