Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Throwing exceptions from static initialization code

Posted on 2002-05-07
6
Medium Priority
?
294 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
[X]
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
6 Comments
 
LVL 6

Accepted Solution

by:
kotan earned 300 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
Technology Partners: 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

Technology Partners: 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …
Suggested Courses

670 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