This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: JLS 14.17 throw unchecked exception in instance block
It's not a secret anymore!