• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Initialization and exceptions

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone !

AFAIK it is possible to throw an exception in an initialization
block or in an instance variable initialization expression
as far as all of its class' constructors declare this exception
provided at least one constructor is available. However an anonymous
class can throw any exception in its initialization blocks/expressions.

Somehow, it doesn't want to work for me :/



This one however compiles:


Could anybody please explain it to me ? :/

What about not checked exceptions ?

Also, I would expect that it doesn't apply to
static initialization block, but since the above
doesn't work for me, I can't check this one.


Cheers,

Adrian
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adrian!

The reason for your problem:
The compiler checks if it is - at least theoretically - possible that the commands within an INSTANCE initializer block are able to complete normally.

Without your if-condition this it not possible, since the exception will be ALWAYS thrown, and therefore

fails to compile. Note: it does neither matter whether this is an initializer block of a named or anonymous class nor if you throw a checked or unchecked exception.

The same is true for STATIC initializer blocks: The compiler checks if it is - at least theoretically - possible that the commands within an static initializer block are able to complete normally. Thus,
will not compile.

Cheers,
Robert
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert,

Thanks for your reply.
Yes - I got to JLS today and found the exact answer
to my question - you were faster

What is interesting:

will generate compile time error while

will not !

This is because of different treatment of if statements
to allow some flag variables, like


Cheers,

Adrian
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic