This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization Blocks

 
anil kumar
Ranch Hand
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

runtime exceptions can be thrown everywhere.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can throw checked exceptions in an instance initializer block:

[ June 15, 2007: Message edited by: Barry Gaunt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks


Thanks

Anil Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic