This is a best practice question I suppose.
When designing functions that will be used within scripts, what is the best way to handle errors that could occur within the function?
For example, say we have a basic function that does X and Y:
Function Test-Function
{
Try
{
<# Something in here that would generate an error #>
}
Catch
{
Throw
}
Return $someReturnResultIWantInMyScript
}
My script calls this function:
Try
{
$ValueIWantFromFunction = Test-Function
}
Catch
{
<# Do something here #>
}
If the Test-Function hits a terminating error, it will throw to the caller. The Try/Catch around my function call in the script will receive this error and hit its own catch. I can then decide what to do.
If I didn't throw an error within the function, the script wouldn't see the terminating error, and then my $ValueIWantFromFunction could contain $Null or something not useful.
Is this a good way of error handling with functions and function calls within scripts? Is there a better way?