• 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 Blocks

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

source
javaCertStudyNotes

is this statement true

initializer blocks cannot throw exceptions.

i think it is false.

They can throw RuntimeExcepions.
am i right?

Thanks

Anil Kumar
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

runtime exceptions can be thrown everywhere.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can throw checked exceptions in an instance initializer block:

[ June 15, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing to remember for exam:

If your static initializer throws any exception, the thrown exception is
wrapped in the java.lang.ExceptionInInitializerError and then thrown.

See this:



java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at generalissues.InitBlockTest.<clinit>(InitBlockTest.java:6)
Exception in thread "main"


Thanks,
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry and chandra

one more thing

In static initiliazer we can't throw Checked Exceptions.

am i right?

Thanks
Anil Kumar
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In static initiliazer we can't throw Checked Exceptions.
am i right?



Static initializer must complete normally.
As Barry gave an example that if instance initializer throws a
checked exception the constructor must declare that exception to be caught.
In case of static initializer, you can place the code that may throw
exception in the try catch block. Like:




Instance init block also can't throw checked exception until you handle it
or declare it in the constructor.


Thanks,
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks


Thanks

Anil Kumar
 
reply
    Bookmark Topic Watch Topic
  • New Topic