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?
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:
return true; and returns there, or 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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With