Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's mock question

 
madhur jain
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
This file does not compile.
the answer says that every constructor shouls declare exception using throws clause
but even after adding throws Exception after R()
it does not compile

[ note from Tom - Please use code tags ]
why?
madhur.
[ September 04, 2002: Message edited by: Thomas Paul ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception isn't generated in the constructor.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why isn't this legal?
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 11.2 Compile-Time Checking of Exceptions
............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).
HTH
Ashish H.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron,
You also need to add a try/catch block to any instance creation expression for class R. Please see the S.main method.

 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my version, I declared main() itself to throw Exception. But if I read that JLS section correctly, none of this matters anyway because the initializer can't call a method that throws an Exception.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Language Specification, Section 14.17, The throw Statement states the following.

If a throw statement is contained in an instance initializer (�8.6), then a compile-time check ensures that either its value is always an unchecked exception or its value is always caught by some try statement that contains it, or the type of the thrown exception (or one of its superclasses) occurs in the throws clause of every constructor of the class.

I agree that this appears to contradict The Java Language Specification, Section 11.2, Compile-Time Checking of Exceptions.

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).

It appears that the second quote should have been qualified by the earlier quote.
Has anyone here emailed this error to the publisher, Addison Wesley?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan, someone just pointed me at the JLS Clarifications and Amendments Page, which resolves this matter definitively.

JLS 11.2
The last two paragraphs of section 11.2 on page 221 should be amended to read. This resolves an apparent discrepancy with JLS 8.6 :
Static initializers (�8.7), class variable initializers, and 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).
An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor. An instance initializer in an anonymous class (�15.9.5) can throw any exceptions.

So an instance initializer is allowed to throw a checked exception. It wouldn't surprise me if many compilers disallow it, since this is presumably a very recent clarification.
I wish Sun would incorporate these clarifications into the online JLS, rather than relegating them to a page that almost nobody will find.
Dan (or anyone else): Does either my code sample or yours compile successfully with the latest 1.4 compiler?
[ September 04, 2002: Message edited by: Ron Newman ]
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah... runs on 1.4.1

Output:
251346 /** BANG **/
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints 6 and then BANG ? I don't see how that can happen. Shouldn't the exception be thrown before the constructor can even begin executing?
[ September 04, 2002: Message edited by: Ron Newman ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No bang here...
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not successfully compile any code in 1.3.1_01 version. But apparently it (Ron's version) compiled in 1.4 version. Would not that be difficult to remember the difference in compilers and answere accordingly in exam...
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony, did you uncomment the throw?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't be that sleepy, Ron...
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ran that code, and it printed "6" and didn't print "BANG" ? If so, I don't understand why.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not the only one confused. I added
System.out.println("Throwing...");
inside m1() and it didn't show up either. Did I overlook anything? Let me fiddle with something...
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awww hell.
I was editing the file at C:\anthony\java\applications which had the classpath. The one inside C:\j2sdk1.4.0\bin had BANG unedited... :roll:
That does it. I'm going home...
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am not sure if I am following you guys. Did this code work under 1.4 or not. I did not work under 1.3....
[ September 04, 2002: Message edited by: Barkat Mardhani ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony -- but does it print "6" before BANG, as Vin Kris claims?
I don't have 1.4 so I can't play with it myself. I sure hope Sun doesn't ever put a question like this on the exam, given that they only recently figured out how they really wanted it to work.
[ September 04, 2002: Message edited by: Ron Newman ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:

Dan (or anyone else): Does either my code sample or yours compile successfully with the latest 1.4 compiler?
[ September 04, 2002: Message edited by: Ron Newman ]

Ron,
Yes, I compiled my code on 1.4.
Thank you for posting the link to the page for the JLS corrections. I didn't even know that it exists.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
Anthony -- but does it print "6" before BANG, as Vin Kris claims?
I don't have 1.4 so I can't play with it myself. I sure hope Sun doesn't ever put a question like this on the exam, given that they only recently figured out how they really wanted it to work.
[ September 04, 2002: Message edited by: Ron Newman ]

Ron,
Opps. I'm rewritting this post completely. Like Anthony, I must have also been too sleepy. When I wrote my previous response I had neglected to add the "throw new Exception()" statement to P.m1().

Prints: 25134
As expected, 6 is not printed.
[ September 05, 2002: Message edited by: Dan Chisholm ]
[ September 05, 2002: Message edited by: Dan Chisholm ]
 
madhur jain
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well looks like this page has the answer
<a href="http://developer.java.sun.com/developer/bugParade/bugs/4409174.html">BUG</a>
madhur.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant a "checked" exception, not "unchecked".
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it works as expected:
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Top of the mornin' to ye laddies! I can see that I left too abruptly yesterday...
Originally posted by Ron Newman:
Anthony -- but does it print "6" before BANG, as Vin Kris claims?

I have exactly the same results as Jose. Honest.
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys... i wasn't clear in the post. my mistake.
I meant to say that BANG is also printed if the throw statement was uncommented. Guess i should have written two separate lines of output.
The output i got with BANG uncommented was ...
25134Exception in thread "main" java.lang.Exception: BANG
at P.m1(S.java:2)
at R.<init>(S.java:19)
at S.main(S.java:29)
[ September 05, 2002: Message edited by: Vin Kris ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vin Kris:
sorry guys... i wasn't clear in the post. my mistake.
I meant to say that BANG is also printed if the throw statement was uncommented. Guess i should have written two separate lines of output.
The output i got with BANG uncommented was ...
25134Exception in thread "main" java.lang.Exception: BANG
at P.m1(S.java:2)
at R.<init>(S.java:19)
at S.main(S.java:29)
[ September 05, 2002: Message edited by: Vin Kris ]

Vin,
Thank you for pointing out that mistake. I made a similar mistake so I rewrote my previous post.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just added to following three sentences to the remark associated with the exam question that started this thread.

Prior to Java 1.4, the code example used in this question would not compile. The bug was fixed in version 1.4. The bug ID is 4409174.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic