PS invoke-sql error catching

I'm using invoke-SQL and it is not catching this error and the program cycles through the all of the tables in the database without telling me there was an issue:
"The statement terminated. The maximum recursion 100 has been exhausted before statement completion."

Is there something wrong with this code? I just need the program to stop and tell me if there's an issue.
$ErrorActionPreference = "Stop"


$CSV = Invoke-Sqlcmd -Query $Query -ErrorAction Stop
catch{ [System.Exception]
    $errorout += "major error, query failed problem"
    $errorout += $error[0]

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Can you be more specific about "this error"? Is the main loop executed (and Invoke-SQL not part of it)?
johnj_01201Author Commented:
The program continued to run as normal, showing no results for most of the tables the recursion error was taking place with. I set the recursion level to 32767 in the SQL query to get rid of the  recursion error, but I thought the try\catch code wrapped around the invoke-sql statement would have caught the error and stopped the program to tell me?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
So your query is producing recursion (a CTE?), and issues with that are not reflected as exception/error? That might be an issue with Invoke-Sql. I'll try to debug that. It might be necessary you show us the (simplified) query you run.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
BTW, the catch doesn't make much sense. [System.Exception] is wrong here: You are adding a string and an ErrorRecord to $errorout.

After catching the error, you continue with the script part "MAIN PROGRAM LOOP STARTS HERE". Is that what you want to do? Providing some more context might be necessary for us to understand.

It would also be interesting to see how the error message should be constructed and displayed (if so).
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Tests run successful. If I get recursion error in Invoke-Sql, the exception handler is triggered. This is my test case:
$ErrorActionPreference = "Stop"
remove-variable errorout

$Query = @"
with cte as 
( select 1 as no
  union all
  select * from cte
select * from cte

$CSV = Invoke-Sqlcmd -Query $Query -ErrorAction Stop -ServerInstance mssqlserver -Database MyDB -Username Me -Password Secret
    $errorout += $error[0]
    Write-Error "major error, query failed problem"

Write-Host "... and the story continues ..."

Open in new window

It halts, because of Write-Error, creating another error entry with the custom message.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.