<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Java Checked Exception and RuntimeException - throw Exception from static block ?</title>
	<link>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/</link>
	<description>Good judgement comes from experience, and experience comes from bad judgement.</description>
	<pubDate>Mon, 01 Dec 2008 21:33:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: prashant</title>
		<link>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/#comment-88</link>
		<dc:creator>prashant</dc:creator>
		<pubDate>Fri, 12 Oct 2007 15:26:28 +0000</pubDate>
		<guid>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/#comment-88</guid>
		<description>Hi,

Good little tiny post.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Good little tiny post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/#comment-87</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Fri, 12 Oct 2007 14:31:46 +0000</pubDate>
		<guid>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/#comment-87</guid>
		<description>Yes. I agreed. Throwing exception in static block is not a good idea :)</description>
		<content:encoded><![CDATA[<p>Yes. I agreed. Throwing exception in static block is not a good idea <img src='http://twit88.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niklas Mehner</title>
		<link>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/#comment-82</link>
		<dc:creator>Niklas Mehner</dc:creator>
		<pubDate>Fri, 12 Oct 2007 09:03:24 +0000</pubDate>
		<guid>http://twit88.com/blog/2007/10/12/java-checked-exception-and-runtimeexception-throw-exception-from-static-block/#comment-82</guid>
		<description>Throwing an exception in a static initializer is a very bad idea. Given the class:

public class StaticClass {
  static {
    Object o = null;
    o.toString();	
  }
  public static void someMethod() {}
}

and using it in the following way:
try {
  StaticClass.someMethod();
} catch (Throwable t) {
  t.printStackTrace();
}
try {
  StaticClass.someMethod();
} catch (Throwable t) {
  t.printStackTrace();
}

you get an ExceptionInInitializerError when invoking it for the first time(caused by a NullPointerException). So you get the information you need to fix the problem.

But on the second invocation you get a java.lang.NoClassDefFoundError: StaticClass. And this does not help you at all.

So if you happen to miss the first exception (which might happen if you have a lot of logging) you will have a big problem finding out, what actually went wrong.

So imho static initializers should be avoided as much as possible. 

If you change the StaticClass to:

public class StaticClass {
  private StaticClass instance;  

  public StaticClass() {
    Object o = null;
    o.toString();	
  }
  public void someMethod() {}

  public synchronized StaticClass getInstance() {
    if (instance == null) {
      instance = new StaticClass()
    }
    return instance;
  }
}

You avoid that problem. Also the code will recover if the failure is a temporary one ("file not found" or something like that). In the first version you have to restart the VM to fix the problem.</description>
		<content:encoded><![CDATA[<p>Throwing an exception in a static initializer is a very bad idea. Given the class:</p>
<p>public class StaticClass {<br />
  static {<br />
    Object o = null;<br />
    o.toString();<br />
  }<br />
  public static void someMethod() {}<br />
}</p>
<p>and using it in the following way:<br />
try {<br />
  StaticClass.someMethod();<br />
} catch (Throwable t) {<br />
  t.printStackTrace();<br />
}<br />
try {<br />
  StaticClass.someMethod();<br />
} catch (Throwable t) {<br />
  t.printStackTrace();<br />
}</p>
<p>you get an ExceptionInInitializerError when invoking it for the first time(caused by a NullPointerException). So you get the information you need to fix the problem.</p>
<p>But on the second invocation you get a java.lang.NoClassDefFoundError: StaticClass. And this does not help you at all.</p>
<p>So if you happen to miss the first exception (which might happen if you have a lot of logging) you will have a big problem finding out, what actually went wrong.</p>
<p>So imho static initializers should be avoided as much as possible. </p>
<p>If you change the StaticClass to:</p>
<p>public class StaticClass {<br />
  private StaticClass instance;  </p>
<p>  public StaticClass() {<br />
    Object o = null;<br />
    o.toString();<br />
  }<br />
  public void someMethod() {}</p>
<p>  public synchronized StaticClass getInstance() {<br />
    if (instance == null) {<br />
      instance = new StaticClass()<br />
    }<br />
    return instance;<br />
  }<br />
}</p>
<p>You avoid that problem. Also the code will recover if the failure is a temporary one (&#8221;file not found&#8221; or something like that). In the first version you have to restart the VM to fix the problem.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
