I get contradictory error messages when trying to set up a try-catch-finally
block in a class method which returns a string.
My code is:
class exampleClass {
[string]Create() {
try {
$rsa_Base64 = "string"
return $rsa_Base64
}
catch {
{...}
}
finally {
Remove-Item -Path $env:TEMP\test.txt
}
}
}
In this situation I get the error: Not all code path returns value within method.
If I move the return
statement to the finally
block I get the error: Flow of control cannot leave a Finally block.
I want to add the finally
block in order to ensure the removal of created files in the try
statement.