In the program I’m currently writing, I find myself doing the following a lot…
Map<String,List<String>> network = loadSerializedObj(file); // null if failed
if(network != null) {
anonNet = util.anonymize(newNet);
} else {
// Some sort of error handling.
System.out.println("Some sort of error message. Exiting...");
System.exit(0);
}
Is there a more succinct way of handling the event that loading the serialized object from file doesn’t work and the method returns null? Any tips at all are welcome. Anywhere I can make this more elegant?
you should make loadSerializedObj throw an exception instead of return null. you can return null when you don’t have anything to return. when something breaks, you should throw an exception.