• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception in instance block  RSS feed

 
vipul jhawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What compile time error do you get?
 
vipul jhawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja

The compile time error i get is "Initializer must be able to complete normally"
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your initializer can't complete normally -- it throws an exception unconditionally, meaning that the constructor can never complete, either. This is against the rules. Try something like this:



That will compile, because as far as the compiler is concerned, the initializer might be able to execute.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!