I have a method in VB.NET which is just a helper for throwing
exceptions. It will always throw an exception and never returns
However the compiler does not detect this function as a terminating
code path and thus I get warning if I use
variables later on in the code that are not initialized via the exception code path.
Function Foo(y as Integer) As Boolean
dim x as boolean
if y > 10
x = 20
else
ThrowHelperFunction("Ouch")
end if
return x
End Function
The warning is that x is not initialized on all code paths.
Update:
There is a way since .NET Standard 2.1 (.NET 5 and newer):
Or conditionally:
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.doesnotreturnattribute?view=net-8.0
Original answer:
I don’t think that you can change that behavior. Instead you can do something like:
That is, the helper function can still do some processing. But it will return an exception instead of throwing it.