proposal: "throw?" #3720
Replies: 12 comments
-
Why would I return an exception from a method invocation? This seems like such a small corner case that it doesn't seem worth it to put time into adding this. |
Beta Was this translation helpful? Give feedback.
-
Why do you use this pattern instead of directly throwing from |
Beta Was this translation helpful? Give feedback.
-
How common a use-case is it that you're been returned an exception which, if not null, you want to throw? I suspect it's almost zero. |
Beta Was this translation helpful? Give feedback.
-
While I agree this is too niche to add to the language. It is a moderately common pattern to move exception creation into helper methods to either:
|
Beta Was this translation helpful? Give feedback.
-
This is actually a pattern I've seen quite a bit. (It's an OK pattern for implementing a pair of TryXyz and Xyz methods when you don't care about the extra allocation of the exception in the TryXyz case.) That being said, I agree with @AlgorithmsAreCool, this is pretty niche. If you're finding yourself implementing this pattern a lot, I think you're better off making an internal extension method for internal static class ExceptionExtensions
{
[DoesNotReturn]
// [StackTraceHidden] -- https://github.com/dotnet/runtime/issues/29681
internal static void Throw(this Exception exception)
=> throw exception;
} Then use it as: CheckInputValue_ImageSize(i_sourceImageSize)?.Throw(); |
Beta Was this translation helpful? Give feedback.
-
@PathogenDavid : That's exactly what I need... if the Another reason for implementing this pattern: It's easier to write unit tests for these check methods. Instead of a try-catch in the unit test, you can just check the returned exception object. |
Beta Was this translation helpful? Give feedback.
-
I'll frequently create factory methods to create exceptions, but the logic controlling whether they throw will always be in the main method. Using factory methods, and moving exception instantiation out of the main flow of the method, gives room for better (more informative) exceptions. So, I'd write the original code thusly: if (!ImageSizeIsValid(i_sourceImageSize)
{
throw InvalidImageSizeError(i_sourceImageSize);
} where the factory method might look something like this: private InvalidOperationException InvalidImageSizeError(int imageSize)
{
var result = new InvalidOperationException($"Image size {imageSize} is invalid");
result.Data[nameof(imageSize)] = imageSize;
return result;
} With respect to the original proposal, I don't believe a |
Beta Was this translation helpful? Give feedback.
-
I do use this approach at other places, but the problem is, that you'll have to double the logic if you want to provide additional information about what is wrong. |
Beta Was this translation helpful? Give feedback.
-
The API was approved yesterday, so I like to hope it'll become available sooner rather than later. |
Beta Was this translation helpful? Give feedback.
-
Can't it work to declare an internal attribute in the correct namespace in source, just like with all compiler-recognized attributes? |
Beta Was this translation helpful? Give feedback.
-
I was hoping so too. It's not a compiler thing, but the Visual Studio debugger does not appear to match it by shape either: Additionally, |
Beta Was this translation helpful? Give feedback.
-
I wonder if they would accept a PR to match by full name rather than by metadata token. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The following code:
could be simplified as
if there was an "throw?" command with the meaning "throw if not null".
Beta Was this translation helpful? Give feedback.
All reactions