Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt Regarding Anonymous Inner Classes

 
Ravi Pinnaboyina
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All constructors must declare the uncaught checked exceptions, if any.

Instance Initializers in anonymous classes can throw any exception.


I couldnt understand the second point, can someone give me an example....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that initializing an anonymous class might throw a checked exception. A throws clause cannot be added to the constructor, because anonymous classes have no named constructers. However, these checked exceptions still need to be caught or declared at some higher level.

For example, the code below has an anonymous class that might throw an exception in its initialization. No throws clause is possible for the anonymous constructor, so the throws class must go elsewhere -- in this case, in the enclosing class's constructor (because this anonymous class is instantiated with the instantiation of the enclosing class).

For details on the troubled history of this, see Throwing Checked Exceptions during Instance Initialization by Doug Dunn.
[ February 21, 2007: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic