aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception from 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 from Initializer block" Watch "Exception from Initializer block" New topic
Author

Exception from Initializer block

Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Hi all,
if an instance initializer block does not catch a checked exception that can occur during its execution, then exception must be declared in throws clause of every constructor...
Now look at the following code---
public class init2{
int i;
{
System.out.println("instance block");
i=10;
throw new NewException();
}

public init2() throws NewException{
System.out.println("constructor");
}

public static void main(String args[])throws Exception{
init2 ob = new init2();
}
}
class NewException extends Exception{
NewException (){}
NewException (String msg){
super(msg);
}
}
This code does not work!
I am not handling the Newexception in initializer block but it has been declared in throws clause of constructor. The wahts wrong?
or i am missing something?
Rashmi
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Actually your previous question is the answer to this one..
Look at the code below
int i;
{
try
{
System.out.println("instance block");
i=10;
}
catch(java.lang.Exception e)
{
System.out.println("Error:" +e.getMessage());
}

}
public Init() throws NewException{
System.out.println("constructor");
}
public static void main(String args[])throws Exception{
Init ob = new Init();
}
}
class NewException extends Exception{
NewException (){}
NewException (String msg){
super(msg);
}
}

Initializer Expression can not pass on checked exceptions, only unchecked ones.If any checked exception is thrown during execution of initializer expression , It must be caught and handled within initializer expression.
Because of this the compiler was complaining in your code.In my code you can see that I have caught & handled the exception as expected.
Hope I was clear.

Originally posted by Rashmi Gunjotikar:
Hi all,
if an instance initializer block does not catch a checked exception that can occur during its execution, then exception must be declared in throws clause of every constructor...
Now look at the following code---
public class init2{
int i;
{
System.out.println("instance block");
i=10;
throw new NewException();
}

public init2() throws NewException{
System.out.println("constructor");
}

public static void main(String args[])throws Exception{
init2 ob = new init2();
}
}
class NewException extends Exception{
NewException (){}
NewException (String msg){
super(msg);
}
}
This code does not work!
I am not handling the Newexception in initializer block but it has been declared in throws clause of constructor. The wahts wrong?
or i am missing something?
Rashmi

Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
OK i understand abt my first Q. But i am still confused wuth the second one...
if an instance initializer block does not catch a checked exception that can occur during its execution, then exception must be declared in throws clause of every constructor...

i am not catching the exception in the initializer, but it's declared as throws clause in constructor. Then what does this stmt actually? or my interpretation is wrong?
i am taking exact meaning of words out of this stmt...
can u explain what it actully means?
thanx in advance.
Rashmi
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346

It is a bug (Bug id #4409174) in Javac ( the compiler which comes wid JDK )
Javac DO NOT allow certain things which JLS says it shld allow
more on this can be found at : http://members.spree.com/education/javachina/Cert/FAQ_SCJP7.htm#som_Q2
This bug is supposed to be fixed in JDK 1.4 release (Merlin)
And for other bugs in Javac et all, check bug database at : http://developer.java.sun.com/developer/bugParade
U can get Jikes , a open source Java compiler , which do not have this bug from : http://www-124.ibm.com/developerworks/opensource/jikes/
As far as original code by Rashmi is concerned , the compiler Javac gives two errors , one of which is due to bug :
init2.java:6: unreported exception NewException; must be caught or declared to b
e thrown
throw new NewException();
^
init2.java:3: initializer must be able to complete normally
{
^
2 errors

The first error is due to bug in Javac .
second error is because given initializer ALWAYS throw checked exception NewException and so never be able to complete normally , this is a compile time error as stated in JLS 2nd ed (�8.6)
for more , check a similar discussion at : http://www.javaranch.com/ubb/Forum24/HTML/011868.html

------------------
Gagan (/^_^\)


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception from Initializer block