Terminating Errors

This topic discusses the method used to report terminating errors. It also discusses how to call the method from within the cmdlet, and it discusses the exceptions that can be returned by the Windows PowerShell runtime when the method is called.

When a terminating error occurs, the cmdlet should report the error by calling the ThrowTerminatingError method. This method allows the cmdlet to send an error record that describes the condition that caused the terminating error. For more information about error records, see Windows PowerShell Error Records.

When the ThrowTerminatingError method is called, the Windows PowerShell runtime permanently stops the execution of the pipeline and throws a PipelineStoppedException exception. Any subsequent attempts to call WriteObject, WriteError, or several other APIs causes those calls to throw a PipelineStoppedException exception.

The PipelineStoppedException exception can also occur if another cmdlet in the pipeline reports a terminating error, if the user has asked to stop the pipeline, or if the pipeline has been halted before completion for any reason. The cmdlet does not need to catch the PipelineStoppedException exception unless it must clean up open resources or its internal state.

Cmdlets can write any number of output objects or non-terminating errors before reporting a terminating error. However, the terminating error permanently stops the pipeline, and no further output, terminating errors, or non-terminating errors can be reported.

Cmdlets can call ThrowTerminatingError only from the thread that called the BeginProcessing, ProcessRecord, or EndProcessing input processing method. Do not attempt to call ThrowTerminatingError or WriteError from another thread. Instead, errors must be communicated back to the main thread.

It is possible for a cmdlet to throw an exception in its implementation of the BeginProcessing, ProcessRecord, or EndProcessing method. Any exception thrown from these methods (except for a few severe error conditions that stop the Windows PowerShell host) is interpreted as a terminating error which stops the pipeline, but not Windows PowerShell as a whole. (This applies only to the main cmdlet thread. Uncaught exceptions in threads spawned by the cmdlet, in general, halt the Windows PowerShell host.) We recommend that you use ThrowTerminatingError rather than throwing an exception because the error record provides additional information about the error condition, which is useful to the end-user. Cmdlets should honor the managed code guideline against catching and handling all exceptions (catch (Exception e)). Convert only exceptions of known and expected types into error records.

See Also



Show:
© 2014 Microsoft