This is a matter of best practice, I suppose.
When designing functions to be used in scripts, what is the best way to handle errors that may occur inside a function?
For example, let's say we have a main function that does X and Y:
Function Test-Function { Try { <
My script calls this function:
Try { $ValueIWantFromFunction = Test-Function } Catch { <
If Test-Function reaches a final error, it will call the caller. Try/Catch catch around my function call in the script will get this error and hit its own catch. Then I can decide what to do.
If I did not select the error inside the function, the script will not see the final error, and then my $ValueIWantFromFunction may contain $Null or something not useful.
Is this a good way to handle errors with functions and function calls inside scripts? Is there a better way?
source share