Expand Minimize

Do not use trap statements

Trap statements are designed for administrators. Try Catch should be used in all written script files. History: Trap was implemented in v1, because it was all the PowerShell team had time to implement.

CheckId SPC019106
TypeName DoNotUseTrapStatement
Severity CriticalWarning
Type PowerShell File

Bad Practice

trap { ... }
Good Practice
try
{}
catch [ExceptionTypeException]
{
  Write-Debug 'An ExceptionTypeException has occurred'
}

Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.