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

Instance Initializer

Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
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

Joined: Mar 22, 2006
Posts: 76
:-)

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....


Bala<br />SCJP 1.4 98%<br />SCBCD 1.3 -- 88%
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Thanks Balaji...

You are awesome!
radhika holani
Greenhorn

Joined: Mar 28, 2006
Posts: 29
I have the same query .
I am not clear with ur answer
Balaji VR
Ranch Hand

Joined: Mar 22, 2006
Posts: 76
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

Joined: Mar 02, 2005
Posts: 580
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

Joined: Mar 28, 2006
Posts: 29
thx. Got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance Initializer