Solved

Throwing exceptions from static initialization code

Posted on 2002-05-07
6
289 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 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
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

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

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