• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Instance Initializer  RSS feed

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have studied in a book which says:
Instance initializer blocks in anonymous classes have greater freedom, they can throw any exception.

I wrote code to verify it:

class MyCheckedException extends Exception {}

class C {

C(int i) throws Exception {}
}

class A {

public static void main(String[] args) {

int i = new C(3) {

{
throw new MyCheckedException();
}

int getI() {

return 2;
}
}.getI();
}
}

Why Am I getting compile time error???
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the statement means :-)

class Sample {

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

Sample() {}

public static void main(String args[]) throws Throwable{
final Sample obj = new Sample() {
{
m();
}
};
}
}
There is no need to declare that exception in your class's constructor.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Balaji...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!