• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JLS 14.17 throw unchecked exception in instance block

 
Ramya Iyer
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 TestInstantiation.java
TestInstantiation.java:8: 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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This gives compile error




But this doesnt...



Goes against the point mentioned in the JLS...as in the posts above..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic