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...
package com.jcp;
import java.io.IOException;
public class
Test {
static int x = getX();
int y = getY();
static int getX() {
System.out.println("execution of static method to initialize static");
return 1;
}
static int getY() {
System.out.println("executed of static method to initialize insatnce");
return 2;
}
{
System.out.println("executed of instance block");
throw new Exception();
}
static {
System.out.println("executed of static block");
}
Test() throws Exception {
System.out.println("constructor called");
try {
System.out.println("i m " + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("constructor finished");
}
public static void main(
String args[]) {
try {
Test t = new Test();
} catch (Exception e) {
}
}
}
If someone could help me as to how to implement the statement in teh book.