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 ?
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

752 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