问题
I've been searching around for this but can't seem to find it. I'm having a script with a try {} catch {} statement. I'd like to add an action if NO error occured.
Eg
try { something }
catch { "Error occured" }
if (!error) {
"No Error Occured"
}
How can I test if no error occured in the statement?
Thanks in advance
Walter
回答1:
Chek the automatic-variable $error
after cleared
$error.clear()
try { something }
catch { "Error occured" }
if (!$error) {
"No Error Occured"
}
回答2:
Another way:
$ErrorOccured = $false
try
{
$ErrorActionPreference = 'Stop'
...something...
}
catch
{
"Error occured"
$ErrorOccured=$true
}
if(!$ErrorOccured) {"No Error Occured"}
.
回答3:
What about,
something
If ($?)
{
"No error"
}
Else
{
"Error"
}
This will work for non terminating errors too.
来源:https://stackoverflow.com/questions/10496885/powershell-error-handling-do-something-if-no-error-occured