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.
In this case you can use the exception catch.
Map<String,List<String>> network = loadSerializedObj(file); // null if failed
try {
anonNet = util.anonymize(newNet);
} catch(NullPointerException npe) {
System.out.println("Some sort of error message. Exiting...");
System.exit(0);
}
but you must specify the util.anonymize to throw the NullPointerException if it does not it yet.
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