Solved

Throwing exceptions from static initialization code

Posted on 2002-05-07
6
287 Views
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 ?
0
Comment
Question by:pgiusto
6 Comments
 
LVL 6

Accepted Solution

by:
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.

Eg.

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..
  }
}
0
 

Expert Comment

by:selsted
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
}
0
 
LVL 16

Expert Comment

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

what do you need this for ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pgiusto
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.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6996280
you can always and everywhere throw RuntimeException (like NullPointerException)
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6996310
And in this case it makes sense to throw
    ExceptionInInitializerError
like this...

-------StaticCode.java-------cut here-------8<-------
public class StaticCode
{
    public static final long[] a = new long[1];
   
    static
    {
        a[0]=Math.round( Math.random() );
       
        if (a[0]==0)
        {
            throw new ExceptionInInitializerError( "...is being thrown when a[0] is initialized to 0" );
        }
    }
   
} /* StaticCode */
-------StaticCode.java-------cut here-------8<-------


-------statictest.java-------cut 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 */
-------statictest.java-------cut here-------8<-------

Greetings,
    </ntr> :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySqlDump not dumping triggers 1 52
web application structure 18 111
Running JavaFX on JDeveloper 12C 1 76
Java List 4 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

679 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