Same exception thrown in else and surrounding catch blocks
I have a snippet of code which checks if the network is available, and a machine is on an AD domain (Two if checks in && configuration). The other side of the coin, I have else if statements for each condition. In here, I throw relevant exceptions if they are not available (I don't expect a machine to not be on the domain etc, this is not a routine event etc).
Problem is, race conditions. I have experienced a few race conditions so they are worth handling. In this case, after the check, the network may go down in which case the code will crash, so I need catch blocks. But then the catch blocks will catch the same type of exceptions as those thrown in the else blocks (those related to no domain or network).
Is this (same exception types in the else and catch blocks) a code smell or perfectly good coding?
EDIT: My code is such:
if (networkIsAvailable && MachineOnDomain) { }
else if !(networkIsAvailable) { throw new networknot开发者_如何学Goavailablexception... }
else if !(machineNotOnDomain // you get the idea
This is surrounded by a try-catch
, catching the same exception types.
Thanks
you can catch different types of exceptions:
try{
int x = 1;
int y = 0;
int z = x /y;
}
catch(ArgumentNullException){
//this will never be reached
}
catch(DivideByZeroException){
// this code will be reached
}
will this help in your case?
精彩评论