Skip to content

Navigation Menu

Sign in
Sign up

I want to try/catch a particular SQL error, allowing others to crash #161

Unanswered
kwein123 asked this question in Q&A
Discussion options

During Invoke-SqlUpdate, I want to catch an error about "Violation of UNIQUE KEY constraint" and handle it on my own. If any other error happens during Invoke-SqlUpdate, I want to let the default (whatever that is) occur.
I have tried every combination of

try {Invoke-SqlUpdate ... -ErrorAction Xyz}
catch {# Do something here}

But I can't ever reach the catch block. I can ignore this AND OTHER errors, or I can exit the function, etc. But I want to get into the catch block - at least for this one error.

(FWIW - I can easily get that error to occur in my code)

You must be logged in to vote

Replies: 1 comment

Comment options

@kwein123
-ErrorAction Stop should do what you want, it will stop the execution of the script, allowing the use of Try / Catch.

try {
 #this was run against a SQLite connection, the table does not exist and the syntax is incorrect... so an error is generated.
 Invoke-SqlUpdate "select top 10 from noexist" -ErrorAction Stop
 Write-Host "This should not be displayed!"
}
catch {
 # $_ is an error record...
 $_.exception
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /