aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's mock question 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 "Dan Watch "Dan New topic
Author

Dan's mock question

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
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

Joined: May 05, 2000
Posts: 13974
The exception isn't generated in the constructor.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But why isn't this legal?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
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

Joined: Jul 02, 2002
Posts: 1865
Ron,
You also need to add a try/catch block to any instance creation expression for class R. Please see the S.main method.



Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Jun 17, 2002
Posts: 154
yeah... runs on 1.4.1

Output:
251346 /** BANG **/
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Mar 22, 2002
Posts: 1055
No bang here...
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 06, 2002
Posts: 1056
Anthony, did you uncomment the throw?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
I can't be that sleepy, Ron...
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You ran that code, and it printed "6" and didn't print "BANG" ? If so, I don't understand why.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Aug 27, 2002
Posts: 49
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

Joined: Jun 06, 2002
Posts: 1056
I think you meant a "checked" exception, not "unchecked".
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
For me it works as expected:


SCJP2. Please Indent your code using UBB Code
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jun 17, 2002
Posts: 154
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 02, 2002
Posts: 1865
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's mock question