Emily L.
Emily L.

Reputation: 5931

Handling non-fatal errors in Java

I've written a program to aid the user in configuring 'mechs for a game. I'm dealing with loading the user's saved data. This data can (and some times does) become partially corrupt (either due to bugs on my side or due to changes in the game data/rules from upstream).

I need to be able to handle this corruption and load as much as possible. To be more specific, the contents of the save file are syntactically correct but semantically corrupt. I can safely parse the file and drop whatever entries that are not semantically OK.

Currently my data parser will just show a modal dialog with an appropriate warning message. However displaying the warning is not the job of the parser and I'm looking for a way of passing this information to the caller.

Some code to show approximately what is going on (in reality there is a bit more going on than this, but this highlights the problem):

class Parser{
    public void parse(XMLNode aNode){
        ...
        if(corrupted) {
            JOptionPane.showMessageDialog(null, "Corrupted data found", 
                "error!", JOptionPane.WARNING_MESSAGE);
            // Keep calm and carry on
        }
    }
}

class UserData{
    static UserData loadFromFile(File aFile){
        UserData data = new UserData();
        Parser parser = new Parser();
        XMLDoc doc = fromXml(aFile);
        for(XMLNode entry : doc.allEntries()){
            data.append(parser.parse(entry));
        }
        return data;
    }
}

The thing here is that bar an IOException or a syntax error in the XML, loadFromFile will always succeed in loading something and this is the wanted behavior. Somehow I just need to pass the information of what (if anything) went wrong to the caller. I could return a Pair<UserData,String> but this doesn't look very pretty. Throwing an exception will not work in this case obviously.

Does any one have any ideas on how to solve this?

Upvotes: 1

Views: 1010

Answers (1)

Ed I
Ed I

Reputation: 7358

Depending on what you are trying to represent, you can use a class, like SQLWarning from the java.sql package. When you have a java.sql.Statement and call executeQuery you get a java.sql.ResultSet and you can then call getWarnings on the result set directly, or even on the statement itself.

You can use an enum, like RefUpdate.Result, from the JGit project. When you have a org.eclipse.jgit.api.Git you can create a FetchCommand, which will provide you with a FetchResult, which will provide you with a collection of TrackingRefUpdates, which will each contain a RefUpdate.Result enum, which can be one of:

FAST_FORWARD
FORCED
IO_FAILURE
LOCK_FAILURE
NEW
NO_CHANGE
NOT_ATTEMPTED
REJECTED
REJECTED_CURRENT_BRANCH
RENAMED

In your case, you could even use a boolean flag:

class UserData {
    public boolean isCorrupt();
}

But since you mentioned there is a bit more than that going on in reality, it really depends on your model of "corrupt". However, you will probably have more options if you have a UserDataReader that you can instantiate, instead of a static utility method.

Upvotes: 1

Related Questions