This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Regarding Anonymous Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt Regarding Anonymous Inner Classes" Watch "Doubt Regarding Anonymous Inner Classes" New topic
Author

Doubt Regarding Anonymous Inner Classes

Ravi Pinnaboyina
Greenhorn

Joined: Feb 19, 2007
Posts: 11
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
wood burning stoves
 
subject: Doubt Regarding Anonymous Inner Classes
 
Similar Threads
Extending inner class
Anonymous classes
JQ+ Inner Class Question
Nested classes
JQplus (Trial Version) question