| description | Missing Try Block |
|---|---|
| ms.date | 04/22/2026 |
| ms.topic | reference |
| title | MissingTryBlock |
Severity Level: Warning
The catch and finally blocks must be preceded by a try block. Without a try block, the
catch and finally are interpreted as commands and result in a runtime error, such as:
"The term 'catch' is not recognized as a name of a cmdlet"
This rule identifies instances where catch or finally blocks are present with out an associated
try block.
Note
This rule is not enabled by default. The user needs to enable it through settings.
Add a try block before the catch and finally blocks.
Note
This rule could result in a false positive as it will fire on user code that violates the rule
AvoidReservedWordsAsFunctionNames for functions named catch or finally:
If you have functions named catch or finally, you can either rename the function or disable
this rule.
catch { "An error occurred." }try { $a = 1 / $b }
catch { "Attempted to divide by zero." }Rules = @{
PSMissingTryBlock = @{
Enable = $true
}
}-
Enable: bool (Default value is$false)Enable or disable the rule during ScriptAnalyzer invocation.