• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception in instance block

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What compile time error do you get?
 
vipul jhawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ilja

The compile time error i get is "Initializer must be able to complete normally"
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic