$error is actually global

Security Briefs

Syndication

This is a follow up to yesterday's post - I found that my understanding of the $error variable was wrong - it's actually global.

So here's what I've got now. It's hard to believe this is the best way to solve this problem. Has anyone come up with anything better?

function restartFoo {
  $oldErrCount = $error.Count
  $procList = get-process foo -ea SilentlyContinue
  if ($error.Count -eq $oldErrCount) { $procList | stop-process }
  c:\bin\foo.exe
}

It really makes me wonder why systems like PowerShell and T-SQL in Sql Server 2005 don't simply leverage the try/catch/finally semantics of a language like Java or C#. Why reinvent the wheel (and do it so badly, in both of these cases)?

UPDATE (25 Jan 2007): it's $error.Count, not $error.Length...

Posted Jan 23 2007, 03:27 PM by keith-brown
Filed under:

Comments

Security Briefs wrote Handling errors in PowerShell scripts
on 01-23-2007 5:28 PM
Jeremy Hannon wrote re: $error is actually global
on 01-23-2007 6:13 PM
I am not sure about PowerShell, but they actually added TRY/CATCH functionality to T-SQL in SQL 2005.
Keith Brown wrote re: $error is actually global
on 01-23-2007 9:52 PM
Yep. It is sooo bad. There is no notion of finally; and try/rethrow is insanely verbose.

So yea, it's SQL 2005 that I'm complaining about :)
William wrote re: $error is actually global
on 02-06-2007 7:55 PM
I think you may just need to add -ea stop to that will throw the exception to your trap.

function Restart-NP
{
trap { continue }
get-process -ea stop notepad | stop-process
notepad.exe
}
William Stacey wrote re: $error is actually global
on 02-06-2007 8:14 PM
Actually, I like this one better. As I don't fancy using exception in this way for normal control logic.

function Restart-NP
{
$pl = get-process -ea silentlycontinue notepad
if ( $pl ) { $pl | stop-process }
notepad.exe
}
Keith Brown wrote re: $error is actually global
on 02-07-2007 6:11 AM
For what I'm doing, that last solution seems best. I didn't consider just ignoring the error code and looking at the result, which should be null if the call to get-process didn't complete. Good call!
Audun Gjerken wrote re: $error is actually global
on 02-11-2007 8:02 AM
I sometimes use the "where-clause" to select the desired object. So far, I've not encountered any problems regarding empty pipes:

>> get-process |
where {$_.name -ieq "notepad"} |
stop-process

(the above pipe should be on a single line)
Pradeep Chellappan wrote re: $error is actually global
on 02-13-2007 2:30 PM
I thought TRY/CATCH is already available in T-SQL for SQL Server 2005.
http://www.databasejournal.com/features/mssql/article.php/3587891
DavidR wrote re: $error is actually global
on 06-22-2007 1:58 AM
You can specify your own error variables with -ev parameter
example:

$procList = get-process foo -ea SilentlyContinue -ev $Err
if (!$Err) { $procList | stop-process }
c:\bin\foo.exe

Add a Comment

(required)  
(optional)
(required)  
Remember Me?