So we have ternary operators. Great! Then there's the ??
operator, which does a coalesce over a nullable
variable.
Example:
string emptyIfNull = strValue ?? "";
Question: Is it possible to implement a simple operator like this for a try-catch?
Example:
string result = CoalesceException(someExpression, "");
public static T CoalesceException<T>(expression, defaultValue)
{
try
{
return evaluate expression; // ?
}
catch
{
return defaultValue;
}
}
Is it possible to implement a method that can be used as easily as possible, or even some kind of coalesce-like operator?
MainModule
, you get exceptions for all x64 processes. This is where this "operator" would kick in. It's for inline exception handling where no specific info about the exception (or bubbling) is necessary.try? fooThatCanThrow ?? defaultValue
in Swift, but not yet in C#. At least, nullability was added in C# 8.0, so it's a step closer to have your short syntax one day.