Throwing exceptions from static initialization code

Posted on 2002-05-07
Last Modified: 2010-03-31
I have a class that has a static initialization code and some static methods:

class X {

  static boolean Method1() {
    return var1;

  static boolean var1;

  static {
     // initialization code that throws a Exception

But I have no choice to catch the Exception. I have to throw it away.

¿ There is a way to declare that the static initialization code throws a Exception ?
Question by:pgiusto
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

kotan earned 100 total points
ID: 6995450
You can't throw an exception in static block but you can store it in a class member variable. When one of the method is being called, you can throw that exception from that method.


class X {
  private static Throwable th;

  static {
     try {
       // code that throws a Exeception
     catch (aaaException e) {
       th = e;

  public X() throws aaaException {
    if (th != null)
      throw th;
    // other codes..

Expert Comment

ID: 6995933
If you want to throw an Exception, you need to write:

public static void X() throws Exception
  // Throws Exception

When you write "throws Exception", you are forced to make a try catch around your method calling X, or make that method throws Exception.

If you want to avoid writing throws Exception, throw a RuntimeException instead. This way, you are not forced to make a try catch.

public static void X()
  // Throws RuntimeException
LVL 16

Expert Comment

ID: 6996234
> There is a way to declare that the static initialization code throws a Exception ?

what do you need this for ?
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Author Comment

ID: 6996273
I need throwing a Exception, because the code effectively throws a Exception. Of course I can use a boolean variable and ask for that variable in every method of my class.
I ask the question because I didn't know if it was possible to throw a Exception from a static initialization block. If it's impossible, OK, let's find another way !!!

Thanks everybody.
LVL 16

Expert Comment

ID: 6996280
you can always and everywhere throw RuntimeException (like NullPointerException)

Expert Comment

ID: 6996310
And in this case it makes sense to throw
like this... here-------8<-------
public class StaticCode
    public static final long[] a = new long[1];
        a[0]=Math.round( Math.random() );
        if (a[0]==0)
            throw new ExceptionInInitializerError( " being thrown when a[0] is initialized to 0" );
} /* StaticCode */ here-------8<------- here-------8<-------
public class statictest
    public static void main( String[] args )
        System.out.println( "Start." );
        long a = StaticCode.a[0];
        System.out.println( "a="+a );
        System.out.println( "Done." );
} /* statictest */ here-------8<-------

    </ntr> :)

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question