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 Exception handling in instance initializer 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 "Exception handling in instance initializer block" Watch "Exception handling in instance initializer block" New topic
Author

Exception handling in instance initializer block

Udit Manektala
Ranch Hand

Joined: Sep 17, 2006
Posts: 30
According to this book (Mughal-Rasmussen Pg 341) :
Exception handling in instance initializer blocks differs from thattin static initializer blmocks in the following respect : the execeution of an instance initializer block can result in an unaught bhecked exception provided the exception is declared in the throws clause of every constrctor in the class.

Then how come this following code compiles fine and runs as expected even thouh I dont have a throws clause for the constructor.


public class Tester {

Tester()
{
System.out.println("in constructor");
}
{
int i=5;
if(i<13){throw new IllegalArgumentException();}
}

public static void main(String args[])
{
System.out.println("test");
Tester t = new Tester();
}
}
Udit Manektala
Ranch Hand

Joined: Sep 17, 2006
Posts: 30
Also, if log System.out.printlns from within the cnostructor and the Instance Initializer block, I find that the II Block gets executed before the constructor. This goes against what my book says (Pg 342). Can anyone explain ?
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Originally posted by Udit Manektala:
Then how come this following code compiles fine and runs as expected even thouh I dont have a throws clause for the constructor.

Howdy Udit,
The exception which you are throwing is IllegalArgumentException which is NOT a checked exception; just an unchecked exception. Therefore, you don't need to mention this in the throws clause. In fact, you can throw this exception from main() without mentioning it in the throws clause of main().

Regards,
Abdul Rehman.


SCJP 5.0 (100%)
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Originally posted by Udit Manektala:
Also, if log System.out.printlns from within the cnostructor and the Instance Initializer block, I find that the II Block gets executed before the constructor. This goes against what my book says (Pg 342). Can anyone explain ?

I have an older edition of the book, for JDK 1.2 (?) even though I am going for SCJP 5 :roll:
In that book, this is explained correctly. You need to read the book carefully; you may have made a mistake in reading.
Generally, in the body of a constructor(), the first statement is a call to the superclass's constructor i.e. the super() statement. After this statement, the next statement may be an alternate constructor call using this(...) statement. If no alternate constructor call is present, the instance initializer blocks & instance variable initializers are executed in the order in which they appear in the source code.
Furthermore, all instance variables in a class are considered to be definitely assigned/initialized after an alternate constructor call.

Hope this will help you in understanding the output of your program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling in instance initializer block