Как продолжить выполнение кода после выброса Exception?
В дополнении к моему вопросу (https://ru.stackoverflow.com/questions/1300872/Как-продолжить-выполнение-кода-после-выброса-exception?noredirect=1#comment2295259_1300872), я поправил код, добавил try/catch в цикле, но при получении Ecxpetion программа завершает свою работу, подскажите пожалуйста, как избежать завершения работы программы, после выброса Exception?
Исправленный код:
public async Task<AuctionRequest[]> GetBids(ICollection<RtbModel> rtb_partners, AuctionModel auctionModel)
{
var bids = new Task<AuctionRequest>[rtb_partners.Count];
int i = 0, j = 0;
int k = 0;
string[] address = { @"http://192.168.56.1:8000/response.json", @"http://192.168.56.1:8000/response4.json", @"http://192.168.56.1:8000/response3.json" };
foreach (var advert in rtb_partners)
{
try
{
if (advert == null)
throw new Exception("Dsp not found");
var protocol = advert.Protocol;
string protocolVersion = protocol switch
{
RtbProtocolEnum.OpenRTB_2_2 => "2.2",
RtbProtocolEnum.OpenRTB_2_3 => "2.3",
RtbProtocolEnum.OpenRTB_2_4 => "2.4",
RtbProtocolEnum.OpenRTB_2_5 => "2.5",
_ => throw new NotSupportedException("RTB protocol not supported")
};
var settings = new RTBSettingsModel(advert.ProtocolOptions);
var bidRequest = BuildBidRequestModel(auctionModel[j++], settings);
bids[i] = ProcessBidRequest(bidRequest, address[k], protocolVersion);
i++;
k++;
}
catch(Exception e)
{
_logger.LogWarning(e.Message);
}
}
return await Task.WhenAll(bids);
}
public async Task<AuctionRequest> ProcessBidRequest(BidRequestModel requestModel, string endpoint, string protocol)
{
try
{
var requestString = JsonConvert.SerializeObject(requestModel);
using var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
{
request.Content = new StringContent(requestString, Encoding.UTF8, "application/json");
}
request.Headers.Add("x-openrtb-version", protocol);
using var response = await _client.SendAsync(request);
if (!response.IsSuccessStatusCode)
throw new BadResponseException(response.ReasonPhrase);
var respString = await response.Content.ReadAsStringAsync();
if (respString.Length == 0)
throw new ArgumentException("The response is empty string.");
return new AuctionRequest(requestModel, JsonConvert.DeserializeObject<BidResponseModel>(respString), protocol);
}
catch (Exception ex)
{
throw ex;
}
}