I have written some code in the try block as follows.
try {
if (server1IPAddress != "") {
if ( InetAddress.getByName(server1IPAddress).isReachable(1000) == false) {
}
InsertUploadedTrancasctionDetails(server1IPAddress, deviceId,
XMLTransactionData);
}
} catch (Exception exception) {
if ((server1IPAddress != server2IPAddress)
&& (server2IPAddress != "")) {
InsertUploadedTrancasctionDetails(server2IPAddress, deviceId,
XMLTransactionData);
}
when we are not able to reach an IP Address i need to move from try block if condtion to catch block ie when IP address reachable is false.
if ( InetAddress.getByName(server1IPAddress).isReachable(1000) == false) {
}
I am not able to make an exception in the if block.
Is there any way to move from the If block to the catch block without making an exception or by making an exception in the if block.
Will any one help me please.
If the code for the not-reachable condition is the same as the code in the exception block, then you should either throw an exception, or refactor the current
catchcode into a method, and call that method from both locations. (Or just repeat the code, but ew.) I’m not really sure what the problem is.FWIW, checking explicitly against
falseis generally frowned upon–negate the condition instead:Catching
Exception, particularly if not logging it, is an anti-pattern, although if you’re wrapping it and/or re-throwing it, it’s not as bad.