HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Proper action when a Java program fails

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
failsprogramjavaactionwhenproper

Problem

I have a program that has to initialize a few big things (connect to a few databases, parse some XML) and without the initialization being successful the program would not be able to continue.

Right now I have my main method throwing just a general Exception

public static void main(String[] args) throws Exception
{
    //Throws numerous types of Exceptions
    WhateverObject we = WhateverObject.getInstance();

    we.doSomething();
}


My question is, is there a better way to handle this? Should I catch the exception and then print out that it failed an exit? Something else maybe? Note, there's no hope of program recovery at this point.

Solution

-
What would the users like? It's usually an error message and a log file with the details and stacktraces for bug reports/support calls/debugging. You can throw exceptions out from main but it's not user-friendly (unless the users are the people who develop the software).

-
Please note that singleton nowadays is rather an antipattern. (WhateverObject.getInstance() looks like a singleton.) They make testing harder and often hide dependencies which leads to spaghetti code which is really hard to work with.

  • What is so bad about singletons?



  • TotT: Using Dependency Injection to Avoid Singletons



  • Eliminating static helper classes



  • The Singleton Pattern

Context

StackExchange Code Review Q#44690, answer score: 2

Revisions (0)

No revisions yet.