Solved

Throwing exceptions from static initialization code

Posted on 2002-05-07
6
283 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
Comment Utility
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
Comment Utility
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_
Comment Utility
> There is a way to declare that the static initialization code throws a Exception ?

what do you need this for ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:pgiusto
Comment Utility
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_
Comment Utility
you can always and everywhere throw RuntimeException (like NullPointerException)
0
 
LVL 4

Expert Comment

by:Neutron
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now