I have a method which have a try catch clause where I parse a mobile phone number. If parsing goes OK, the code outside the try continues to execute. If parsing encounters an error, I enter the catch and raise the error.
All this was OK until I got a request to check for another phone number.
I am not sure how to do it because later in the code I need at least one correct phone number (its not important which one).
If I put both parsing inside one try, I have a problem if first one is wrong and second is good because exception will be raised anyway.
try {
model.mobilePhone = PhoneParser.Parse(m.mobile);
model.alternativePhoneNumber = PhoneParser.Parse(m.alternativePhoneNumber);
}
catch (Exception) {
_log.LogWarning("Error while parsing the phone number")
}
return model;
Maybe something like this? Try inside a catch?
try {
model.mobilePhone = PhoneParser.Parse(m.mobile);
}
catch (Exception) {
try {
model.alternativePhoneNumber = PhoneParser.Parse(m.alternativePhoneNumber);
}
catch (Exception) {
_log.LogWarning("Error while parsing the alternative phone number")
}
_log.LogWarning("Error while parsing the mobile phone number")
}
return model;