Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

exception handling in initializers

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from khalid mughal
section 8.2 page 263


if and instance initializer block does not catch a checked exception that can occur during its execution, then the exception must be declared in the throws clause of every constructor in the class.This does not apply to static initializer blocks, which must catch and handle the checked excpetion in the block as no constructor is involved in class initialization.Instance initializer blocks in anonymuous classes have greater freedom; they can throw any exception


i am using jdk1.3
the compiler complains if i throw checked exception from an instance initializer, no matter i declare them in the throws clause of the constructor or not.
can somebody throw some light on this ??
------------------
KS
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the JLS Section 8.6 Instance Initializers
It clearly says


An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor. An instance initializer in an anonymous class (�15.9.5) can throw any exceptions.


So the workaround will be to declare that exception in the throws clause of ALL the class constructors.
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Kalpesh Soni
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replying ajit
but i tried declaring in the constructor
and it doesnt work!

------------------
KS
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ajith,
I am sorry I don't understand the quote.
From my understanding the below codes should work.
<code>
import java.io.*;
public class z {
z() throws FileNotFoundException {
}
{
FileInputStream fis = new FileInputStream("a.txt");
}
public static void main(String args[]) {
System.out.println("main in z");
}
}
</code>
But it fails for
z.java:7: Exception java.io.FileNotFoundException can't be thrown in initializer
.
FileInputStream fis = new FileInputStream("a.txt");
^
1 error
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think JDK 1.3 should support this. There was a discussion about this in Marcus' website. We concluded that it should be available in the latest version i believe.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalpesh,
I also got an error trying to compile your code. I don't think adding a throws clause to the constructor can help because an instance initializer is executed before the ctor; so there is no way to handle the thrown exception unless you specifically catch it in the initializer.

The above outputs:

I think that JLS §8.6 which Ajith quoted is wrong. With or without the explicit ctor; the compiler produces an error unless the instance initializer uses a try/catch block to catch the exception.

------------------
Jane Griscti
Sun Certified Java 2 Programmer
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JDK 1.3.0, still getting the same error. Probably will be fixed in future versions..?

------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just checked Sun's bug site; turns out this is bug 4409174. It supposedly fixed in JDK 1.4.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
 
Kalpesh Soni
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replying guys

------------------
KS
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!