• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Initialzer's

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, Sorry for the error in the questions IT is editited correctly now
___________________________________________________________________________

1.Static and Instance variable initializer expressions.
Cannot pass on the checked exceptions. Must catch and handle them.

2.Static Initialzer
Cannot pass on the checked exceptions. Must catch and handle them.

Q1.why cannot we pass checked exception's to static and instance VARIBLE initializer.
(The word PASS is what i cannot understand. How can someone pass exception in this context(intiliazer)).

___________________________________________________________________________

Q2.can we pass exceptions from constructor to constructor.?

___________________________________________________________________________

3.Instance initializer blocks.

Instance Initializers in anonymous classes can throw any exception

Q3.What does it mean by the above statement?

Q. IF the above code is correct then why does this not compile?
new Runnable()
{
{
synchronized(this)
{wait(); //throws checked exception
}
}//instance block
public void run()
}

___________________________________________________________________________

Please explain,Thank you

[ July 30, 2007: Message edited by: saravana.T kumar ]
[ July 30, 2007: Message edited by: saravana.T kumar ]
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static initializers: These are static blocks that are declared within a class with keyword static { .... } and are used to initialize the static memebers of the class, like static methods, static blocks cannot access the instance members of the class.

Instance variable initialiazers: These are instance blocks {....} that are used to initialize the instance variables of the class and like instance methods can access the static and instance members of the class.



Now checked exceptions are compile time exceptions and they cannot be thrown out of the above two blocks via the throw keyword, they have to be caught and handled within the blocks. Just imagine, static block is executed whhen the class is loaded if JVM would have allowed us to throw exceptions out of these blocks then who would handle them, JVM cannot handle user exceptions thrown from these blocks.

Example:


The above code will cause compiler error:

Now it will work.
I could not understand your last question on , Could you please explain in detail and re-phrase your question.
Thanks
Deepak
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
Now checked exceptions are compile time exceptions and they cannot be thrown out of the above two blocks via the throw keyword, they have to be caught and handled within the blocks.

You can throw checked exceptions in an instance initializer block if you declare the exception on the constructor(s).
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred,
Your explanation made me think the code inside the instance block is simply moved to the constructor and hence when we change the signature of the constructor to throws IOException there will be no compilation error in instance block even if the IOException is not caught in it. But that was not the case.I modified the code to include a overloaded constructor which led to compilation error in instance block and again modifying the construtor to throw IOException resolved it.

Does this mean each and every constructor must throw all the checked exceptions that are thrown in instance blocks if it doesnt handle it?

Also when excatly the code placed in the instance block is executed, before or after the construtor invocation?

Thanks
Deepak
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Execeptions thrown in instance initializers in annonymouse inner classes muse be
a) Either caught in the initializer.
OR
b) The entire class definition must be within try {...} catch(){}
OR
c) The method that defines the annonymouse inner class must throw the exception.

Also the code that you had mentioned will wait for ever after invoking wait()
Thanks
Deepak
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
Does this mean each and every constructor must throw all the checked exceptions that are thrown in instance blocks if it doesnt handle it?

Yes, since the initializer block is always executed, every constructor may throw the exception(s).

Also when excatly the code placed in the instance block is executed, before or after the construtor invocation?

Between the invocation of the super constructor and the body of the constructor.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does this mean each and every constructor must throw all the checked exceptions that are thrown in instance blocks if it doesnt handle it?



Another insight to this, instance initialization block becomes part of your constructor. compiler inserts all instance initialization block at the first of all constructors unless that constructor calls another constructor using this keyword. So eventually instance initialization block gets called from all constructors.

Thanks.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sahid, from your statement

Another insight to this, instance initialization block becomes part of your constructor. compiler inserts all instance initialization block at the first of all constructors unless that constructor calls another constructor using this keyword. So eventually instance initialization block gets called from all constructors.



Initialzation block would get called from all the constructors, if we call the constructors explicitly. Explicitly I mean here is not calling a constructor from another, but using keyword "new".
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Kota:
Explicitly I mean here is not calling a constructor from another, but using keyword "new".

And during the processing of new, the constructor is called.
 
I want my playground back. Here, I'll give you this tiny ad for it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic