5

I have a static initialization block. It sets up logging to a file. If something goes wrong, I simply want to break out of the static block. Is this possible? I know I could use an if/else approach, but using a simple break would make the code much more readable.

3
  • Would this leave your class in an inconsistent/unusable state? Commented Jul 12, 2011 at 16:02
  • Yes, it would. It is a logging class. So if this fails, it could still write the logging output to the console, but the logging to file would be unusable. Commented Jul 12, 2011 at 16:03
  • 1
    What I was getting at was, if your class was in an unusable state, it may not be loadable by the classloader (or something along those lines), causing all sorts of problems. Just keep in mind that all static final variables should be initialized, regardless... Commented Jul 12, 2011 at 16:20

6 Answers 6

9

Your static block can call a method

static { init(); }

private static void init() {
     // do something
     if(test) return;
     // do something
}
Sign up to request clarification or add additional context in comments.

Comments

4

You probably want to catch all exceptions:

static {
    try {
        // Initialization
    }
    catch (Exception exception) {
        // Not much can be done here
    }
}

But beware: loading the class won't fail, but some or all static fields could be in an inconsistent state.

Comments

2

Is this what you're looking for?

label:
{
  // blah blah
  break label;
}

6 Comments

Can you break to a label that is below the break statement?
The label has to be at the start of a block, the place to break to is at the end of the block.
@user48 you don't break to label, you break the label
A break with label is only useful if it is nested more than one level deeper than the label. Otherwise, it's just bad coding style since it can be avoided in a more readable way.
You cannot use labels to branch to an arbitrary place in the code (so no, you can't go to a label that is below the current code). That is a valid use of the label - when it hits the label, it's going to go to the label, then skip over the section that was labelled (so, // blah blah won't get executed a second time).
|
1
  • if it is an exception, use try{throw new Exception();}catch
  • if it is normal processing, use if-then-else or switch
    eventually you could use labels, but IMHO it is a very bad style:

    //boolean condition;
    static {
     label:
     {
      System.out.println("1");
      if(condition) break label;
      System.out.println("2");
     }
    }
    

1 Comment

please don't use HTML code tags. just indent your code (4 spaces or 8 inside a list) and code will be displayed nicely.
0

How about try/catch ?

try{}catch(){}

Comments

0

In my opinion, a static block is not different from any other block in terms of flow control strategies to use. You can use BREAK wherever if you find it more readable (in your static block also) but the general assumption is that it makes the code less readable in fact, and that a IF ELSE approach is better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.