Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 809
  • Last Modified:

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?
Try
{
$CountCSV = Invoke-Sqlcmd -Query $Query -Username "test" -Password "test" -ErrorAction Stop -QueryTimeout 65535  
}
catch{
    $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
$error
$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.
0
johnj_01201
Asked:
johnj_01201
  • 2
  • 2
2 Solutions
 
footechCommented:
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.
0
 
QlemoC++ DeveloperCommented:
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.
0
 
johnj_01201Author Commented:
Thanks. I guess the try\catch is not as simple as I thought. I will research and learn more about it.
0
 
QlemoC++ DeveloperCommented:
Proper error handling is indeed a more advanced topic ...
0
 
footechCommented:
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).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now