© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
6 replies
Trenyc

✅ 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.)

            _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.");
            _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# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Why doesn't this try catch, catch exception.
C#CC# / help
10mo ago
✅ Try catch exception.
C#CC# / help
13mo ago
exception subclass for try-catch block [Answered]
C#CC# / help
4y ago
✅ Issue with try/catch block
C#CC# / help
10mo ago