This is an issue that I learnt about the _hard_ way. It took me ages to
figure out exactly what "NoClassDefFoundError" means. In my experience the
ExceptionInIntializer exception is not thrown too often. The javadocs say
that it is thrown when a static initializer fails, but I don't often see
that exception thrown, more often the NoClassDefFoundError is thrown. Your
explanation as to why it is not thrown makes sense.
I think if I had a rule that said always log all exceptions thrown from
static blocks, that would have saved me a lot of pain, and confusion because
the actual exception in initializer stack trace, I don't often see.
I think the real issue is that when you get a NoClassDefFoundError what it
means it that there was an exception in the static bits of the class
concerned. Not actually that the class' definition was not found.
On Thu, May 29, 2008 at 9:46 PM, Dr Heinz M. Kabutz
<[EMAIL PROTECTED]> wrote:
>
> Hi Jeff,
>
> if you throw a RuntimeException, it will automatically be converted to
> an ExceptionInInitializerError and this will prevent your class from
> loading. Subsequent attempts to use the class will produce a
> NoClassDefFoundError, which can be really confusing. You'll be
> searching your class path for missing jar files!
>
> Presumably you want to use this in the context of a Singleton, so I
> would prefer the following code (or move away from statics altogether,
> even better):
>
> public class JeffsCrazyClass {
> private volatile static JeffsCrazyClass instance;
>
> public static JeffsCrazyClass getInstance() {
> if (instance == null) {
> synchronized(JeffsCrazyClass.class) {
> if (instance == null) {
> instance = new JeffsCrazyClass();
> }
> }
> }
> return instance;
> }
>
> private JeffsCrazyClass() {
> try {
> if (ServiceManager.service == null) {
> Context ctx = new InitialContext();
> //perform some lookup from JNDI
> }
> } catch (Exception e) {
> log.log(Priority.ERROR, "Exception"+ e.getMessage());
> throw new IllegalStateException("JeffsCrazyClass could not be
> instantiated", e);
> }
> }
> }
>
> The volatile is important of course, due to the double-checked-locking
> visibility bug.
>
> Regards
>
> Heinz
> --
> Dr Heinz M. Kabutz (PhD CompSci)
> Author of "The Java(tm) Specialists' Newsletter"
> Sun Java Champion
> http://www.javaspecialists.eu
> Tel: +30 69 72 850 460
> Skype: kabutz
>
>
>
> Jeff Mutonho wrote:
> > What are the best practises when it comes to handling exceptions in a
> > static initializer. Say you have code that looks like :
> >
> > static {
> > try {
> > if (ServiceManager.service == null) {
> > Context ctx = new InitialContext();
> > //perform some lookup from JNDI
> > }
> > } catch (Exception e) {
> > log.log(Priority.ERROR, "Exception"+ e.getMessage());
> >
> > }
> > }
> >
> >
> > lol.log is not good enough , as it doesn't make sense to proceed if
> > something wrong happened with the lookup code in the try block.I've
> > seen a few debates on the net and it seems some propose that after
> > logging in the catch block , then one should throw an
> > ExceptionInInitializer as shown below :
> >
> > catch (Exception e) {
> > log.log(Priority.ERROR, "Exception"+ e.getMessage());
> > throw new ExceptionInInitializerError(e);
> > }
> >
> > Other posts on the net go as far as saying one should throw
> > RuntimeException or even use System.exit(1)
> >
> > What are your thoughts on this? Any best practises that you could
> > recommend?
> >
> >
> >
> >
> >
> >
> > --
> >
> > "Don't take the name of root in vain."
> >
> > Jeff Mutonho
> > Cape Town
> > South Africa
> >
> > GoogleTalk : ejbengine
> > Skype : ejbengine
> > Registered Linux user number 366042
> > >
>
> >
>
--
see my blog:
http://analysis102.blogspot.com
http://audiblethoughts.blogspot.com
http://outsideofficehours.blogspot.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---