Forums Register Login

Doubt Regarding Anonymous Inner Classes

+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
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 ]
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 780 times.
Similar Threads
JQ+ Inner Class Question
JQplus (Trial Version) question
Extending inner class
Nested classes
Anonymous classes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:55:03.