File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JLS 14.17 throw unchecked exception in instance block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JLS 14.17 throw unchecked exception in instance block" Watch "JLS 14.17 throw unchecked exception in instance block" New topic

JLS 14.17 throw unchecked exception in instance block

Ramya Iyer
Ranch Hand

Joined: Jul 29, 2003
Posts: 66
Hi All,

As per JLS 14.17

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.

To understand this and also the instantiation process, I had written a small program...I am getting a compile error at line 8,

javac initializer must be able to complete normally
Why am I getting this error when I am throwing an unchecked exception. Any pointers?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Congratulations! It looks as if you found yourself a bug in the compiler.
Make yourself famous and tell Sun about it. (This was a bug way back, and was fixed in about 1.2 - it looks as if it has come back again).

Just send them this code to demonstrate the problem:

[ October 18, 2005: Message edited by: Barry Gaunt ]

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

Joined: Jul 04, 2004
Posts: 980

This gives compile error

But this doesnt...

Goes against the point mentioned in the in the posts above..
I agree. Here's the link:
subject: JLS 14.17 throw unchecked exception in instance block