Just to clarify my own thoughts on this:
I'm not really asking for anything that works any specific way. I'm just
looking for something that works to handle errors.
I'm OK with the "trap" command although it could be documented in help a bit
more.
I'm OK with a different class of errors where I have to modify an
$ErrorActionPreference variable to change the default behaviour when
necessary.
The VBScript "On Error Goto" stuff wasn't pretty, but it worked even for
ADSI.
But, in the case of ADSI, where I use New-Object to access
DirectoryServices.DirectoryEntry objects and then later use a method on that
object which then fails, there is no error handling. The script aborts with
an error, returning a CmdletInvocationException.
I would love something that works well, but before that PowerShell needs
something that works at all. Trap currently works for a few errors, but
doesn't appear to work for all errors.
If I could get the script to continue on, maybe I could cludge something
together.
Thanks,
- Dan
"Adam Milazzo" <adamm@san.rr.com> wrote in message
news:OUkAGKp5GHA.508@TK2MSFTNGP06.phx.gbl...
> Dan Metzler wrote:
>> One thing I noticed is that the $error[0] returns a
>> CmdletInvocationException instead of an ErrorRecord.
>>
>> It looks like error handling is getting extremely complicated. Is this
>> going to change?
> I've noticed that a lot of people seem confused by the trap mechanism. It
> has unintuitive scoping, and I think people might be better served by a
> more standard try/catch/finally mechanism. It works kind of like Visual
> Basic's "On Error Goto ..." mechanism, which sucks. I realize many unix
> shells handle errors this way, with signal handlers, but it's not a good
> mechanism either.
>
> Also, the trap mechanism only catches fatal errors, I think. Errors that
> don't prevent the pipeline from continuing won't be caught. There's also
> no way to redirect the errors to anything but a file. That would make it
> easier to handle non-terminating errors from a given command. Perhaps
> there could be a 2| operator analogous to the 2> operator.
>
> I agree that error handling needs to be improved, perhaps even reworked
> entirely...