try using blocks #1874
Replies: 17 comments
-
Expression-bodied statements are very ambiguous as to their intent. Do they mean "return this value"? do they mean "just execute this"? Either are possible interpretations. With expression-bodied members, it is clear what has to happen as it's the only body statement for the member. With statements, it is entirely unclear. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi
What do you say about that? |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem Using try (...)
{
try (...) { }
catch (...) { } // Clearly catches the second try
}
try (...)
{
try (...) { }
}
catch (...) { } // Clearly catches the first try
try (...)
try (...) ...
catch (...) ... // Um...
try (...) => ...
try (...) => ...
catch (...) => ... // Hm...? No indenting to show the ambiguity the compiler faces, as C# indentation is ignored. You may intend for it to be: try (...)
try (...) => ...
catch (...) => ... // Catches second try, not first or maybe: try (...)
try (...) => ...
catch (...) => ... // Catches first try, not second But that would involve the compiler assuming that and could cause bugs and crashes and errors not being crashed when you thought they would be. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure the few characters plus level of indentation saved is worth much. The number of times I have personally seen a situation where I have immediately nested try's and using's has been few if any. Additionally its a bit unclear about the whether the using code setup and Dispose are in or out of the Try-catch scope. Would exceptions in CreateDisposable() or Dispose() be caught or no? Furthermore when does a finally occur relative to the using's Dispose(), before or after. |
Beta Was this translation helpful? Give feedback.
-
@lazyloader @DanFTRX |
Beta Was this translation helpful? Give feedback.
-
As a language comparison, in Java the equivalent of the
is effectively translated to
(although it has some extra magic in it so that if the innermost block threw an exception, but then I've sometimes wanted to attach my own
(I think this would only make sense for However, I think the 'bang-for-buck' for this feature is pretty small: it would have been nice if the |
Beta Was this translation helpful? Give feedback.
-
@juffindell |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem Try using doesn't really expand well into multi using blocks, if anything the try would need to be locked at the end. using (var disposable1)
using (var disposable2)
try
{
} At which point @juffindell 's syntax becomes the more ideal. |
Beta Was this translation helpful? Give feedback.
-
@DanFTRX
|
Beta Was this translation helpful? Give feedback.
-
You can already write this: using(SqlConnection conn = new SqlConnection(connString))
try
{
conn.Open();
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} You could even write: using(SqlConnection conn = new SqlConnection(connString)) try
{
conn.Open();
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} if you really wanted to... Now... whether or not you should do that is up to you and your team's coding preferences. I would not personally permit that in any of my codebases. But there's nothing stopping you from using those forms today. |
Beta Was this translation helpful? Give feedback.
-
Looking at your code examples, I wonder whether another solution might be possible. using(SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} |
Beta Was this translation helpful? Give feedback.
-
@DavidArno |
Beta Was this translation helpful? Give feedback.
-
I can't imagine that this comes up so frequently that three additional characters would be considered so onerous: using(SqlConnection conn = new SqlConnection(connString)) try
{
conn.Open();
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} |
Beta Was this translation helpful? Give feedback.
-
Maybe. I've no idea how often folk nest I agree that that |
Beta Was this translation helpful? Give feedback.
-
using(SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} should be try
{
using(SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
}
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} not using(SqlConnection conn = new SqlConnection(connString))
{
try
{
conn.Open();
}
catch(SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
}
} And I really interest in this syntax because it shorten the try block indent |
Beta Was this translation helpful? Give feedback.
-
The net effect is the same. C# 8.0 is already (likely) getting the following, which takes care of the indentation concern: try {
using var conn = new SqlConnection(connString);
conn.Open();
}
catch (SqlException sqlEx) {
MessageBox.Show(sqlEx.Message);
} |
Beta Was this translation helpful? Give feedback.
-
@HaloFour It a little bit difference that if the And if we allow using(var conn = new SqlConnection(connString))
{
conn.Open();
}
catch (SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
} Which is shorter. The point difference is, would it allow access to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We have to use
try catch
withusing
like this:or like this:
I suggest to shorten this to:
And I suggest to use expression bodied
try catch
blocks if they contain only one statement to make them shorter like this:Beta Was this translation helpful? Give feedback.
All reactions