File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes question about anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "question about anonymous class" Watch "question about anonymous class" New topic
Author

question about anonymous class

josie chen
Greenhorn

Joined: Mar 20, 2003
Posts: 6
according to JLS, instance initializers in anonymous class can throw any exception.
I don't know if my imagination of those code is correct or not. Since all I try cause compile error.
this is my testing code:
class AnonyExcptTest{
public void test() {
new AnonyExcptTest(){
{
// I suppose to add exception here.
throw new Exception();
}
public void mytest() throws Exception{
throw new Exception();
}
};
}
}
Who can help me to figure out?
Thanks a lot!
josie@prome.net
[ April 02, 2003: Message edited by: josie chen ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 949
Don't know if this has anything to do with your question but you doing a "new" without assigning the result to anything.


Please ignore post, I have no idea what I am talking about.
josie chen
Greenhorn

Joined: Mar 20, 2003
Posts: 6
sorry, maybe my testing code is incorrect. could you give me an example to explain 'instance initializers in anonymous class can throw any exception.'
thank you very much.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652

So, why does the language allow this? Well, let's say you're writing a class, and need the option to throw an exception during object construction. If the class is anonymous, you can't declara a new constructor for it - the only way you can throw an exception during initialization is in an instance initializer (or field initializer, which is pretty similar). Normally in order to throw a checked exception in an initializer, you'd be required to declare all constructors of your class so that they'd declare the new checked exception. But for an anonymous class, this is impossible - no way to declare constructors at all. This is OK since there's no way for anyone to invoke a constructor of your anonymous class anyway, except by calling the code that declares it, which must either catch the exception or declare it. (Note my main() method declares it throws Exception.) Any other code that calls your code will know about the checked exception, even though no constructor was declared to throw it.


"I'm not back." - Bill Harding, Twister
josie chen
Greenhorn

Joined: Mar 20, 2003
Posts: 6
Very appreciate you, Jim.
The explaination is very clear.
I think I shoudn't rely on develper tool too much. Cause the results are different.
 
IntelliJ Java IDE
 
subject: question about anonymous class
 
Threads others viewed
Instance Initializer, throw statement
Instance Initializer
Still Stacks
What is an Instance Initializer?
Stacks
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture