Replies: 12 comments
-
I disagree with this proposal, because as a general rule I say: do not derive information from the fact that an exception is thrown. Let exceptions be what they are: a break from the normal flow. If you need information that something is not filled in, call upon functionality that just tells you it is empty. Also: try and finally perform fine, but catch can have a performance impact if it goes off. Also you might be ignoring a different error than intended. You probably really want to know that your value is empty. But this code might hide other problems. So it promotes error hiding, so instead of a clear exception message it goes wrong and just continues. No error message and the customer finds out something is wrong, before the programmer can and the programmer spends hours and hours finding your bug, which should have been prevented by just letting the exception go off, not ignoring it. Exceptions are not our enemy. Exceptions are our friends. Don't write code to simply ignore them. I feel your proposal would promote these bad practices. |
Beta Was this translation helpful? Give feedback.
-
@janjoostvanzon That's a fair point, and I can definitely see how this could be abused. That being said, there are plenty of times where exceptions are unavoidable (networking issues, 3rd party libraries, etc.), and your use case involves setting a default or cached value & moving on. Additionally, this proposal doesn't necessarily ignore exceptions, as it provides a mechanism to do something useful with it. What if we could structure the syntax in a way that allows for catching specific exception types, and doesn't force you to catch
|
Beta Was this translation helpful? Give feedback.
-
Please do not ever do this. |
Beta Was this translation helpful? Give feedback.
-
@trbenning In short: Be strict in code: either everything should go right, or the intermediate result should be discarded, show user vague error, log complete error for yourself. Analyse the error, solve the core of the problem.
I would like to go into this a little more constructively, but see first paragraph. |
Beta Was this translation helpful? Give feedback.
-
I really don't like the proposed syntax. GetValue() ?? null : ex => Console.WriteLine("Exception thrown!") To me, this reads something like:
|
Beta Was this translation helpful? Give feedback.
-
You can do this with a library function. /// <summary>
/// Tries to get a value from the given function. If it throws an exception of the given type,
/// it is caught and null is returned. An optional exception logic function is also run.
/// </summary>
/// <typeparam name="TException">The type of the exception to catch.</typeparam>
/// <typeparam name="TReturn">The return type expected.</typeparam>
/// <param name="function">The function to try and get a return value from.</param>
/// <param name="exceptionLogic">The exception logic to optionally run.</param>
/// <returns>The value from the given function, or the type's default value.</returns>
[DebuggerNonUserCode]
public static TReturn TryGetValue<TException, TReturn>(
Func<TReturn> function,
Action<TException> exceptionLogic = null
)
where TException : Exception
{
try
{
return function();
}
catch (TException e)
{
exceptionLogic?.Invoke(e);
return default(TReturn);
}
} Usage: var a = TryGetValue(() => SomethingThatMightThrow(), e => LogException(e)); |
Beta Was this translation helpful? Give feedback.
-
@svick I agree. Using I've edited my examples to use this alternative operator. |
Beta Was this translation helpful? Give feedback.
-
Perhaps it would be best just to omit the try. That would be safer and even more concise. |
Beta Was this translation helpful? Give feedback.
-
Similar proposal here that may have some benefits:
|
Beta Was this translation helpful? Give feedback.
-
it is better to be: |
Beta Was this translation helpful? Give feedback.
-
This is highly unlikely to be something the language adds. Catching and ignoring all exceptions is an anti-pattern, and generally indicates some pretty significant design problems. The language shouldn't really be encouraging this. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Consider this code:
It would be nice if we could reduce the footprint with some syntax sugar, similar to how null coalescing operators work. Something like:
Beta Was this translation helpful? Give feedback.
All reactions