I have implemented some static throw helper methods in a net-standard 2.1 (C# 8.0) project.
[DoesNotReturn]
public static void InvalidOperation(String message, String caller, String path, Int32 line)
{
throw new InvalidOperationException($"{message}\n{caller}|{path}|{line}");
}
I want to use them in a NET 5.0 project like:
public static BitmapSource GetBitmapSource(String uri)
{
if (Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out Uri? _uri))
{
return new BitmapImage(_uri);
}
Throw.InvalidOperation( $"Invalid uri {uri}.");
}
But the the compiler still returns the error CS0161: "Not all code paths return a value"
puplic? Ok, you should not use throwing exceptions with methods likeThrow.InvalidOperation( $"Invalid uri {uri}.");You may do exception fablic and use like:throw MyExceptionGenerator.InvalidOperation($"Invalid uri {uri}.");, but best practice is generate Exceptions bynewoperator - you should generate the new exception class and use like:throw new MyInvalidOperaionException($"Invalid uri {uri}.")- Leszek Mazurthrow ThrowHelper.Whatever()or switch the order you perform your checks - pinkfloydx33