[Proposal] Add "suppress" operator in try-catch #8801
Replies: 7 comments
-
There's a simple solution - add a comment Most analysers which warn on empty catch blocks, don't warn if there's a comment. |
Beta Was this translation helpful? Give feedback.
-
See previous similar discussion at #220. |
Beta Was this translation helpful? Give feedback.
-
Might as well propose a rethrow keyword try
{
DoSomeWork();
}
rethrow (OperationCanceledException)
suppress (SqlException sqlEx) when sqlEx.ErrorCode == 123
catch (Exception ex)
{
ShowError(ex.Message);
}
finally
{
Finalize();
} |
Beta Was this translation helpful? Give feedback.
-
That would be a separate proposal, if you feel the need for such a thing. |
Beta Was this translation helpful? Give feedback.
-
I have an ex.Ignore extension method that does nothing and is conditional debug |
Beta Was this translation helpful? Give feedback.
-
Didn't it just try
{
DoSomeWork();
}
catch(SqlException sqlEx) when sqlEx.ErrorCode == 123 {}
catch (Exception ex) when !(e is OperationCanceledException)
{
ShowError(ex.Message);
}
finally
{
Finalize();
} Or maybe try
{
DoSomeWork();
}
catch (Exception ex) when !(ex is OperationCanceledException) && !(ex is SqlException sqlEx && sqlEx.ErrorCode == 123)
{
ShowError(ex.Message);
}
finally
{
Finalize();
}
// pattern matching?
try
{
DoSomeWork();
}
catch (Exception ex) when ex not OperationCanceledException or SqlException with { ErrorCode : 123 }
{
ShowError(ex.Message);
}
finally
{
Finalize();
} ??? I know Maybe try
{
DoSomeWork();
}
catch (OperationCanceledException) throw; // can shorthand the throw in place of when, and it will work like your rethrow, should not push new stack
catch (Exception ex) when !(ex is SqlException sqlEx && sqlEx.ErrorCode == 123)
{
ShowError(ex.Message);
}
finally
{
Finalize();
} |
Beta Was this translation helpful? Give feedback.
-
This is how I currently deal with this:
My Intent was expressed. I did not forget to write the catch block, I clearly want to discard the exception. Also all the analysis tools I've been using are happy with this, and I have plenty. Also no "do nothing" method with conditional compilation required. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
In try-catch code block user can suppress exception explicitly with
suppress
operator.Motivation
Quite often there is a need to suppress some exception in try-catch, especially
OperationCanceledException
. For this we need to create emptycatch
block. It may looks ambiguously, should it be suppressed or just somone forgot to handle it. Introducingsuppress
operator indicate explicitly exception must be suppresed.Beta Was this translation helpful? Give feedback.
All reactions