Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7981463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:22:47+00:00 2026-06-04T10:22:47+00:00

I find myself choosing between the two following design patterns a lot: static {

  • 0

I find myself choosing between the two following design patterns a lot:

static {
    try {
        foo();
    }
    catch(Exception exc) {
        throw new RuntimeException(exc.getMessage());
    }
}

and

TheConstructor() throws SomeException {
    if(alreadyInited) {
        return;
    }
    alreadyInited = true;
    foo();
}

The problem is I really do want to initialize stuff once per class – so statically, I think – such as setting up a logger, loading some map from a file, etc. – and I really do want the program to halt if this operation fails. Both of these design patterns seem kludgey (the first one more obviously so), so I’m wondering if there’s a better way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:22:49+00:00Added an answer on June 4, 2026 at 10:22 am

    Is allowing exceptions in static class constructors to escape a proper design pattern?

    I would say No.

    First, wrappering a checked exception in that context means that if something goes wrong you will get a class initialization failure.

    • An exception that propagates out of the class initalizer cannot be caught and handled directly.

    • An uncaught exception during class initialization is not recoverable. Even if you could catch and handle the exception, the class and (I think) all others that depend on it / they will be left in a state where they cannot be initialized, and hence cannot be used. That’s effectively “game over” for (at least) the classloader, and possibly the entire JVM.

    The bigger picture is that this probably represents an inappropriate use of statics. There are better ways to handle this problem; e.g.

    • Do the initialization using an explicit call (in a place where you can catch and handle the exception), rather than relying on class initialization.

    • Get rid of the static (and potentially the singleton class that wraps it) and use dependency injection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two data structures that I continually find myself choosing between when
I find myself often writing code following the pattern: foo xs = map snd
I find myself writing a lot of code like putStr foo (bar 1) (bar
I find myself doing the following a lot to define return values from ruby
I find myself using Resharper's convert to auto property refactoring a lot to remove
I find myself working through a lot of other peoples' code nowadays. I noticed
I find myself handling exceptions without specifying an exception type when I call code
I find myself checking to see if javascript elements exist a lot using: if
I find myself rarely using object oriented principles when I design applications. I am
I find myself writing a lot of reusable code because I couldn't find reusable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.