• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance Initializer

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The execution of an instance initializer block can result in an uncaught checked exception, provided the exception is declared in the throws clause of every constructor in the class.

I wrote code to verify above statement.
class MyCheckedException extends Exception {}

class A {

A() throws MyCheckedException {

System.out.println("Constructor");
}

{ // Instance initializer block-1

throw new MyCheckedException();
}

public static void main(String[] args) {

try {

new A();
} catch (Exception e) {}
}
}

Why am I getting compile time error.
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-)

The below is what Mugalsan says under "Exception Handling in Instance Initializer Blocks"..

class Sample {

{
m();
}

public void m() throws Exception {
throw new Exception();
}

Sample() throws Exception {}

public static void main(String args[]) throws Exception{
Sample obj = new Sample();
}
}
He says "the execution of an instance initializer block can result in an uncaught checked exception,"
The STRESS is over "THE EXECUTION OF....."
Not,
"the instance initializer block can result in an uncaught checked exception"
Hope you got the difference....
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Balaji...

You are awesome!
 
radhika holani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same query .
I am not clear with ur answer
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radhika

Just read the last 4 lines. Im pasting them again for you! You can easily get that.

He says "the execution of an instance initializer block can result in an uncaught checked exception,"
The STRESS is over "THE EXECUTION OF....."
Not,
"the instance initializer block can result in an uncaught checked exception"
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Girish

Whenever a new instance is created, non-static block(i.e Instance initializer) is executed, followed by the constructor.

But in your program, in the non-static block you are explicility throwing exception, which results in bypassing the call to super class constructor.
So there is no chance for instance to be created.

since Exception is throwing explicility by using throw, it will be detect at compile time itself.

If you catch the Exception at non-static block,compiler wont complain you



correct me if i am wrong
 
radhika holani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx. Got it
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic