File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt Regarding Anonymous Inner Classes" Watch "Doubt Regarding Anonymous Inner Classes" New topic

Doubt Regarding Anonymous Inner Classes

Ravi Pinnaboyina

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

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
I agree. Here's the link:
subject: Doubt Regarding Anonymous Inner Classes
It's not a secret anymore!