exception in instance block

vipul jhawar

Greenhorn

Posts: 5

posted 12 years ago

Hi

I was just reading khaled mughal about intializers. What i comprehended was :

1. If an exception is thrown in the static block than it should be handles there itself and thrown. 2.If an exception is thrown in the instance initialize block, the book says :- " the execution of an instance initializer block can result in an uncaught excption, provided the exception is declare din teh throws clause of every constructor".

I could not simulate the second point and got a compile time error if tried to do it.Below is the code i used... please if someone could elaborate on this...

If someone could help me as to how to implement the statement in teh book.

jh ;-)

john guthrie

Ranch Hand

Posts: 124

posted 12 years ago

i'd say your reference was wrong. static initializers are run at class-load time, which may or may not coincide with object instantiation. so having constructors throw unchecked exceptions won't cover a static initialization.

vipul jhawar

Greenhorn

Posts: 5

posted 12 years ago

Hi john

What i had written was that it is true that we need to handle any exception generated in the static intializer block have to be handles within itself only but when we talk about instance initializer bloacks is it possible to use the throw xxxException in the instance initializer block and than handle it at the constructor level or not?

Or let me put in a single line....

How is exception handling done in an instance intializer block and how is it different from exception handling in a static initializer block? If you refer to Khaled Mughal the two are different? Can anyone explain how?

jh ;-)

Ilja Preuss

author
Sheriff

Posts: 14112

posted 12 years ago

What compile time error do you get?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

vipul jhawar

Greenhorn

Posts: 5

posted 12 years ago

Hi Ilja

The compile time error i get is "Initializer must be able to complete normally"