functionDo-Something { # function uses internal error handling try { Get-Process-Name NotThereOhWell -ErrorAction Stop } # catch this error type catch [Microsoft.PowerShell.Commands.ProcessCommandException] { $oldE = $_.Exception
# handle the error, OR SHOWN HERE: issue a new exception to the caller $newE = New-Object-TypeName System.InvalidOperationException('Do-Something: A fatal error occured', $oldE) Throw$newE } }
# function will encounter an internal error # error message shows error message generated by function instead Do-Something
Message Originalmessage ------- --------------- Do-Something: A fatal error occured Cannot find aprocesswiththe name "NotThereOhWell". Verify theprocess name and call the cmdlet again.