Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.31 KB

File metadata and controls

60 lines (42 loc) · 1.31 KB
description Missing Try Block
ms.date 07/21/2026
ms.topic reference
title MissingTryBlock

MissingTryBlock

Severity Level: Warning

Default state: Disabled

Description

This rule identifies instances where catch or finally blocks are present without an associated 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"

To prevent this error, add a try block before the catch and finally blocks.

Note

This rule is triggered by functions named catch or finally. However, creating functions with those names violates the AvoidReservedWordsAsFunctionNames rule. If you have functions named catch or finally, you can either rename the function or disable this rule.

Example

Noncompliant

catch { "An error occurred." }

Compliant

try { $a = 1 / $b }
catch { "Attempted to divide by zero." }

Configuration

Rules = @{
    PSMissingTryBlock = @{
        Enable = $true
    }
}

Parameters

  • Enable: bool (Default value is $false)

    Enable or disable the rule during ScriptAnalyzer invocation.