C
C#3mo ago
mixels

✅ Why is this try/catch block preventing lines after it from firing when an exception is caught?

I have this foreach block with a try/catch inside it. Can someone please help me understand why the logger line for initialization completing isn't executing when an exception occurs inside the foreach try/catch? I know it's bad form to control flow with exceptions. This is a call to a dependency service which unfortunately doesn't give me a good way to tell if the ConnectAsync method succeeds or fails, so I don't really have a choice. (Also I'd want to catch exceptions regardless.)
c#
_logger.LogInformation("Beginning initialization of ClusterChat");

foreach (var client in _clients)
{
bool authenticated = false;
try
{
await client.Item2.ConnectAsync();
authenticated = await client.Item2.AuthenticateAsync(client.Item1.RconPassword);
}
catch (Exception ex)
{
_logger.LogError(ex, $"Initialization of ClusterChat server {client.Item1.Name} with key {client.Item1.Key} failed.");
continue;
}
if (authenticated)
{
_logger.LogInformation($"ClusterChat successfully initialized for server {client.Item1.Name}.");
}
else
{
_logger.LogError($"Initialization of ClusterChat server {client.Item1.Key} failed.");
_clients.Remove(client);
continue;
}
}

// This logger line isn't executing when an exception is caught above...?
_logger.LogInformation("Initialization of ClusterChat complete.");
c#
_logger.LogInformation("Beginning initialization of ClusterChat");

foreach (var client in _clients)
{
bool authenticated = false;
try
{
await client.Item2.ConnectAsync();
authenticated = await client.Item2.AuthenticateAsync(client.Item1.RconPassword);
}
catch (Exception ex)
{
_logger.LogError(ex, $"Initialization of ClusterChat server {client.Item1.Name} with key {client.Item1.Key} failed.");
continue;
}
if (authenticated)
{
_logger.LogInformation($"ClusterChat successfully initialized for server {client.Item1.Name}.");
}
else
{
_logger.LogError($"Initialization of ClusterChat server {client.Item1.Key} failed.");
_clients.Remove(client);
continue;
}
}

// This logger line isn't executing when an exception is caught above...?
_logger.LogInformation("Initialization of ClusterChat complete.");
4 Replies
cap5lut
cap5lut3mo ago
because of the continue;, this tells the loop, to stop the current iteration of the loop and continue with the next (because in the else branch the continue; is the last statement, its actually not even needed there, because its at the end of the iteration code anyway in will proceed to the next iteration)
mixels
mixelsOP3mo ago
Yeah that's true, but I don't know why that would stop the logger line that's outside the foreach block from executing. Do you know why that logger line doesn't execute?
cap5lut
cap5lut3mo ago
oh, i didnt even notice that after the loop, because u said inside the foreach from the given code that can only happen if anything after the catch block is throwing an exception
MODiX
MODiX3mo ago
cap5lut
REPL Result: Success
Console.WriteLine("before");
foreach (var num in Enumerable.Range(0, 10))
{
try
{
if (num % 2 == 0) throw new Exception();
}
catch (Exception e)
{
Console.WriteLine($"thrown on {num}");
continue;
}
Console.WriteLine($"processed {num}");
}
Console.WriteLine("after");
Console.WriteLine("before");
foreach (var num in Enumerable.Range(0, 10))
{
try
{
if (num % 2 == 0) throw new Exception();
}
catch (Exception e)
{
Console.WriteLine($"thrown on {num}");
continue;
}
Console.WriteLine($"processed {num}");
}
Console.WriteLine("after");
Console Output
before
thrown on 0
processed 1
thrown on 2
processed 3
thrown on 4
processed 5
thrown on 6
processed 7
thrown on 8
processed 9
after
before
thrown on 0
processed 1
thrown on 2
processed 3
thrown on 4
processed 5
thrown on 6
processed 7
thrown on 8
processed 9
after
Compile: 470.842ms | Execution: 80.890ms | React with ❌ to remove this embed.
Want results from more Discord servers?
Add your server