aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initialization Blocks" Watch "Initialization Blocks" New topic
Author

Initialization Blocks

anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Jun 04, 2007
Posts: 377
Hi,

runtime exceptions can be thrown everywhere.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You can throw checked exceptions in an instance initializer block:

[ June 15, 2007: 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.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707

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

Joined: Feb 23, 2007
Posts: 447
Thanks


Thanks

Anil Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization Blocks
 
Similar Threads
aop rules engine
difference between throw and throws
finally block may not contain try catch
Try / Catch Statements
Re :Results of mock test