Saw an interesting piece of code recently, and my mind made a snap judgement that it’d be like something I’d see on TDWTF.
However, before I start changing code I didn’t write in case there is a reason for it, I figured I’d ask here.
public function SomeFunction() As SomeType
Dim someResult As New SomeType
// Some code here, where some result can't become nothing.
try
return someResult
catch ex As Exception
// Some logging code
return Nothing
end try
end function
My first impression is that it would be impossible for a simple return with no other operations on the same line to throw an exception, and that this code is unnecessary.
However no compiler warning about unreachable code, so it did have me wondering if it’s at all possible.
Is it? If so, what would be a case it could happen?
No I don’t think it’s possible. The compiler simply doesn’t know that this statement cannot fail, so it cannot issue a warning.