This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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...
 
Acetylsalicylic acid is aspirin. This could be handy too:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!