Skip to content

Latest commit

 

History

History
65 lines (45 loc) · 1.45 KB

File metadata and controls

65 lines (45 loc) · 1.45 KB
description Missing Try Block
ms.date 04/22/2026
ms.topic reference
title MissingTryBlock

MissingTryBlock

Severity Level: Warning

Description

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.

How

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.

Example

Wrong

catch { "An error occurred." }

Correct

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.