I'm reformatting some legacy code that I don't fully understand, and in it there are several variable assignments that conditionally assign variables to the output of one of two formatting functions, using exception catching like so:
String myString;
try {
myString= foo(x);
} catch (Exception e) {
myString= bar(x);
}
This seems like an abuse of exception handling, and anyway it's a lot of repeated boilerplate code for a lot of variable assignments. Without digging into foo to identify the conditions that might cause exceptions, can I simplify this using a ternary operator expression? I.e. something like this:
String myString = foo(x) ? foo(x) : bar(x)
but catching the exception that might be thrown by foo(x). Is there a way to do this in this one-liner? Or if not, is there a better one-line expression that chooses between two assignments based on a possible exception? I am using Java 8.
One could use lazy evaluation as follows:
String myString = attempt(() -> foo(x), () -> bar(x));
public static <T> T attempt(Supplier<T> a, Supplier<T> b) {
try {
return a.get();
} catch (Exception e) {
return b.get();
}
}
It is not nice and just would be one stage in the entire refactoring.
One pattern for such constructs you saw would be a result message of a process that maybe throws an IOException.
An ugly pattern would be a NullPointerException or such, when data is optional. Then
a complexer redesign with Optional.ofNullable and .map to another Optional might be feasible.
In short I do not see a clean way.
Washcloth answer is already clear. Just wanna add a bit though about your statement:
it's a lot of repeated boilerplate code for a lot of variable assignments.
If you don't want to assign a variable repeatedly, you can create a new method to return String value as below:
String myString = assign(x);
String assign(String x) {
try {
return foo(x);
} catch (Exception e) {
return bar(x);
}
}
You only need to assign the variable once with the above method.
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