File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Classes & Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Anonymous Classes & Exception Handling" Watch "Anonymous Classes & Exception Handling" New topic
Author

Anonymous Classes & Exception Handling

Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223

Static initializers (�8.7), class variable initializers, and instance initializers or instance variable initializers within named classes and interfaces (�8.3.2), must not result in a checked exception; if one does, a compile-time error occurs. No such restriction applies to instance initializers or instance variable initializers within anonymous classes (�15.9.5).



15.9.5.1 Anonymous Constructors
.....
In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.

Can anyone tell me how to achieve what is stated above.
Thx


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Beats me!
Logically, this should compile, but it doesn't, at least in 1.3.1:

The error is:
AnonyClass.java:8: unreported exception java.lang.Exception; must be caught or declared to be thrown
throw new Exception();
^


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
I know it really confuses me. I read in some old posts that this is a bug and it was supposed to be corrected in this version (jdk 1.4.1)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
hey Shishio you do not have to do nothing at all. The compiler will create the proper contructor.


SCJP2. Please Indent your code using UBB Code
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well, this is the way I understand it. According to the JLS:
An anonymous class cannot have an explicitly declared constructor. Instead, the compiler must automatically provide an anonymous constructor for the anonymous class. The form of the anonymous constructor of an anonymous class C with direct superclass S is as follows:

it goes on with explanation on how the AUTOMATICALLY COMPILER provided anonymous constructor is done.
then it says as Shishio quoted:
In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.

So I understand it like this, if the compiler itself provides or creates or generates the anonymous constructor, it adds to the throws clause - in all cases - all the checked exceptions thrown by the explicit superclass constructor, and all checked exceptions thrown by any instance initializers or instancee variable initializers of the anonymous class
1- If the compiler automatically provides that in the anonymous constructor... then:
Static initializers (�8.7), class variable initializers, and instance initializers or instance variable initializers within named classes and interfaces (�8.3.2), must not result in a checked exception; if one does, a compile-time error occurs. No such restriction applies to instance initializers or instance variable initializers within anonymous classes (�15.9.5).

I might have just mis-interpretted the whole paragraph, but this is what I don't like about JLS, it's very specific, very technical, but VERY hard to understand at some points... maybe it's my English, it's not my first language still... Hope I made a point not just lost the point
[ October 26, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Jose,
The problem occurs with the anonymous classes.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi all,
The question remains. How do you report a checked exception thrown in an instance initializer in an anonymous class.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
What do you mean report it?? Do you mean handle it? or declare it? catch it? or say that it throws it?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Jose, what is the difference between your example (which compiles) and mine (which doesn't)?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ron, your class compiles in 1.4.1_01!
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Originally posted by Barry Gaunt:
Ron, your class compiles in 1.4.1_01!
-Barry

I'm using java 1.4.1-b21 and it doesn't compile.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Ron your code compiled in 1.4.0_01-ea
_____________________________________

Shishio
new Base() { };
is an anonymous class extending from Base
______________________________________

As Alfred quoted from JLS the compiler creates the proper constructor:
"Son$1() throws java.lang.Exception;"
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Originally posted by Jose Botella:
Ron your code compiled in 1.4.0_01-ea
_____________________________________

Shishio
new Base() { };
is an anonymous class extending from Base
______________________________________

As Alfred quoted from JLS the compiler creates the proper constructor:
"Son$1() throws java.lang.Exception;"

My question was about throwing an exception in an instance intializer inside the anonymous class.
I know that new Base() {} is an anonymous class but if you try to change its definition to the following you'll get a compile error:
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Ok Shishio, I guess - hope - I got you this time. The code below show the difference in the behavior of the compiler in case of trying to throw an exception within an instance initializer of a top level class / local class and an anonymous class.

The code doesn't compile and ONLY complain about line 7, BUT it doesn't complain about line 2 or line 10.
Read the quotes from the JLS I had in the above post and if the quotes together with the explanation above and this code tells you something and make things clear, then fine, if not then I'll be glad to explain it and show you what JLS means at this VERY specific point.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Ah, forgot to say, Shishio, put in mind that code in instance initializers are included by the compiler at the begining of the constructor code, right after the super() call... SO, if code in instance initializer results in an exception, this exception should be declared in the throws clause of the constructor, and that's why the code above didn't compile although Exception was declared in the throws clause of the method...
BUT in case of anonymous classes, the compiler generates the constructor and add any exception might be thrown in any initializer of the class or the super class, compiler does that on your behalf, and that's why line 10 doesn't generate a compiler error...
Hope it's clear now
Sun LiWei
Ranch Hand

Joined: Aug 10, 2002
Posts: 49

it compiles in
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)
[ October 28, 2002: Message edited by: Sun LiWei ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Anonymous Classes & Exception Handling
 
Similar Threads
Annonymous classes
Constructor vs instance initializer
Anonymous constructors
Dan's mock question
What is an Instance Initializer?