Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this code outside my "using" statement unreachable?

Tags:

c#

If an exception is thrown in a using statement, it should immediately call the finally block, and dispose of the object. So the rest of the try block should not execute, right?

 public static async Task<bool> AddCharName(string id, string name)
        {
            using (var dbConn = new DbConn())
            {
                await dbConn.Connection.OpenAsync();

                command = "UPDATE user SET name = @name WHERE name IS NULL AND id = @id";

                using (MySqlCommand update = dbConn.Connection.CreateCommand())
                {
                    update.CommandText = command;
                    update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
                    update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
                    await update.ExecuteNonQueryAsync();
                    return true;
                }
            }
            return false;
        }

In my example, the return false is giving the "Unreachable code detected" warning. Am I correct to dismiss this as a mistake? Like I am thinking if an error is thrown in the using (MySqlCommand upd... well then it should return false right?

like image 578
Buretto Avatar asked Mar 31 '26 08:03

Buretto


2 Answers

Indeed using is syntactical sugar for try-finally but as there is no catch block. In other words you have:

MySqlCommand update = dbConn.Connection.CreateCommand();
try
{
    update.CommandText = command;
    update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
    update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
    await update.ExecuteNonQueryAsync();
    return true;
}
finally
{
    update.Dispose();
}

Note how there is no catch, therefore your code either:

  1. Reaches return true; and returns there, or
  2. throws an exception. The finally block gets executed disposing the object, then the exception immediately bubbles up to the caller (crashing if there is no handler).

In neither case does execution go past your return true; statement. The same is of course true with the outer using block as well.


Now, what you most likely want to do is catch exceptions that happen. The quick and dirty way would be to wrap everything in a try-catch, though depending on the application you may want to be more granular about where you catch what and how:

try
{
    using (var dbConn = new DbConn())
    {
        await dbConn.Connection.OpenAsync();

        command = "UPDATE user SET name = @name WHERE name IS NULL AND id = @id";

        using (MySqlCommand update = dbConn.Connection.CreateCommand())
        {
            update.CommandText = command;
            update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
            update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
            await update.ExecuteNonQueryAsync();
            return true;
        }
    }
}
catch (MySqlException)
{
    return false;
}
like image 149
lc. Avatar answered Apr 02 '26 21:04

lc.


Code Unreachable because you already has a return true above.

use try catch

try
            {
                using (var dbConn = new DbConn())
                {
                    await dbConn.Connection.OpenAsync();

                    command = "UPDATE user SET name = @name WHERE name IS NULL AND id = @id";

                    using (MySqlCommand update = dbConn.Connection.CreateCommand())
                    {
                        update.CommandText = command;
                        update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
                        update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
                        await update.ExecuteNonQueryAsync();
                        return true;
                    }
                }
            }
            catch (Exception er)
            {
                return false;
            }
like image 39
Vijunav Vastivch Avatar answered Apr 02 '26 22:04

Vijunav Vastivch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!