This simple script will (besides on screen error) write :
NON-TERMINATING
NT 2/2 : success
TERMINATING
T 1/2 : success
As you can see, and as explained, it is not possible to grab a non terminating error within a try catch
But, throwing a terminating error will end the script.
I would like to avoid, each time I am using those commands, to add either :
Surrounding try/catch statements
Adding -ErrorAction SilentlyContinue
I would like to be able to send a terminating error that does not end the script, using a self made module.
A terminating error won’t end the script when caught, but you have send-Terror both inside and outside of your try{} block (although you’ve misspelt it in the try{} block). The send-Terror that’s not in the try{} block won’t be caught and will terminate the script.
function test {
throw 'Whoops!'
}
try {
test
}
catch {
Write-Warning 'Something went wrong with test.'
}
Write-Output "Now I'm going to sleep."
Start-Sleep -Seconds 10
Write-Output "Now I'm awake again."
Yes, but my question is how do I write function with a terminating error that
Will not stop my script without try/catch statement (or using -erroraction)
AND that can still be caught within a try/catch if desired
As the example I have highlighted, within a script, this line will NOT terminate a script invoke-restmethod https://fake.url
But, if desired, it can be caught in a Try/Catch try {invoke-restmethod https://fake.url} catch { "Yes ! This code is ran" }
Another way to resolve this, would be to be able to determine if my last command did raise a non terminating error.
Of course, I could do a $error.count before / after to see if the number of $errors grew, but I would like my scripts to stay as clean as possible. This code is meant to be ran in a very large number of scripts.
Thank you for both your welcome and for answering me
The difference here is that there are two types of terminating errors: statement-terminating which is what you see with Invoke-RestMethod and script-terminating which is what throw generates.
If you want your function to generate a statement-terminating error, you can use $PSCmdlet.ThrowTerminatingError().
That won’t stop the script, but it will be caught in a try/catch block.