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.
Your static block can call a method
static { init(); }
private static void init() {
// do something
if(test) return;
// do something
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With