powershell 3+ try\catch error message output???

This code works, but no matter what I try I cannot get $error or $erroroutput to show the error messages. I can type them manually in the console\output sections of the PS IDE when the program stops to see what the errors are.
Any ideas?
$CountCSV = Invoke-Sqlcmd -Query $Query -Username "test" -Password "test" -ErrorAction Stop -QueryTimeout 65535  
    $errorout += $error[0]
#This next line outputs the text in the quotes, but I cannot get it to do anything else
    Write-Error "CountCSV major error, query failed problem"
#this next line only shows the text in the quotes:
    Write-Error "CountCSV major error, query failed problem" $errorout
#nothing here shows up at all
            write-host $errorout

Again, I can type $error to see an error message and $errorout to see another error message. One is generic and the other is detailed. For example if there's a query time-out, one will basically say there's a networking issue while the other actual tells the query exceeded the timeout period. I would like to shows these 2 outputs automatically instead of having to type them to see what went wrong.
From what I've read on the Internet it should be working as expected, but it is acting like $error and $errorout are empty in the above example.

I've also used the same Try\Catch code in several invoke-sql sections of the program and none of them display the desired information.
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.

Write-Error "CountCSV major error, query failed problem" $errorout
won't work.  In fact that should generate an error.  You can only give it one message to display.
In testing it appears your problem (that $error and $errorout don't output anything in the catch block) is caused by this issue.  Can't say for certain, but maybe it's because PS doesn't handle errors within a catch block well.  That's pure speculation on my part, since I don't know how PS handles errors within a catch block, but it seems to fit the behavior.

Is $errorout defined earlier as an empty array (e.g. $errorout = @() )?
If not then there could be errors when trying to add to it with +=.

Write-Host really only works to output a string, not the entire object, so be conscious of what you output with it.  If you try to output an object with it, it will try to convert it to a string, so that could explain the difference you're seeing between the error messages.
To start off with error handling, I probably wouldn't recommend using Write-Error to begin with.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I've used Write-Error in http://www.experts-exchange.com/Q_28325801.html just for demonstration purposes for that specific test case - because you told the recursion error does not appear, which it does. It generates a new error, and if errors in a catch block occur execution is halted. (You can, of course, nest try/catch to achieve error handling in error handling.)

Keep in mind $error contains all errors (exceptions) which were triggered in the PS session's lifetime, unless $error.Clear() is called. Dumping that var will always supply more info than using only the most recent entry, $error[0] (which is, btw, the same $_ in the catch block).

Also be aware that an object of $error is of type ErrorRecord and contains much more than only an error message. That should be used for proper handling and reporting. And that again requires that you think carefully about what should happen in each particular case.
The common methods are to collect error information in a var (as you did with $errorout), and dump that later.
Or check the  error category, and stop the script, e.g. with the Write-Error cmdlet.

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
johnj_01201Author Commented:
Thanks. I guess the try\catch is not as simple as I thought. I will research and learn more about it.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Proper error handling is indeed a more advanced topic ...
I can't remember where I've read it, but I believe I've seen it said a few times that error handling can easily double the size of your script, and take twice as long to write as the main script portion (or more).
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.