Skip to content

Commit

Permalink
(GH-10798) Clarify how to find exception names in about_Trap
Browse files Browse the repository at this point in the history
Prior to this change, the `about_Trap` topic explained how to
use specific exceptions with a `trap` statement, but not how
to discover the exception names for that purpose.

This change:

- Adds prose and example to the section "Trapping specific errors"
  that covers how to get the full name of an exception
- Resolves #10798
- Fixes AB#200723
  • Loading branch information
michaeltlombardi committed Jan 17, 2024
1 parent 1c0f904 commit f2efd9e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
24 changes: 24 additions & 0 deletions reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ The **CommandNotFoundException** error type inherits from the
**System.Exception** type. This statement traps any errors raised by unknown
commands. It also traps other error types.

You can find the exception type for an error by inspecting the error object.
The following example shows how to get the full name of the exception for the
last error in a session:

```powershell
nonsenseString
$Error[0].Exception.GetType().FullName
```

```Output
nonsenseString : The term 'nonsenseString' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:1
+ nonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (nonsenseString:String) []
, CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
System.Management.Automation.CommandNotFoundException
```

You can have more than one `trap` statement in a script. Only one `trap`
statement can trap each error type. When a terminating error occurs, PowerShell
searches for the `trap` with the most specific match, starting in the current
Expand Down
18 changes: 18 additions & 0 deletions reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ The **CommandNotFoundException** error type inherits from the
**System.Exception** type. This statement traps any errors raised by unknown
commands. It also traps other error types.

You can find the exception type for an error by inspecting the error object.
The following example shows how to get the full name of the exception for the
last error in a session:

```powershell
nonsenseString
$Error[0].Exception.GetType().FullName
```

```Output
nonsenseString: The term 'nonsenseString' is not recognized as a name of a
cmdlet, function, script file, or executable program. Check the spelling
of the name, or if a path was included, verify that the path is correct
and try again.
System.Management.Automation.CommandNotFoundException
```

You can have more than one `trap` statement in a script. Only one `trap`
statement can trap each error type. When a terminating error occurs, PowerShell
searches for the `trap` with the most specific match, starting in the current
Expand Down
18 changes: 18 additions & 0 deletions reference/7.3/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ The **CommandNotFoundException** error type inherits from the
**System.Exception** type. This statement traps any errors raised by unknown
commands. It also traps other error types.

You can find the exception type for an error by inspecting the error object.
The following example shows how to get the full name of the exception for the
last error in a session:

```powershell
nonsenseString
$Error[0].Exception.GetType().FullName
```

```Output
nonsenseString: The term 'nonsenseString' is not recognized as a name of a
cmdlet, function, script file, or executable program. Check the spelling
of the name, or if a path was included, verify that the path is correct
and try again.
System.Management.Automation.CommandNotFoundException
```

You can have more than one `trap` statement in a script. Only one `trap`
statement can trap each error type. When a terminating error occurs, PowerShell
searches for the `trap` with the most specific match, starting in the current
Expand Down
18 changes: 18 additions & 0 deletions reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ The **CommandNotFoundException** error type inherits from the
**System.Exception** type. This statement traps any errors raised by unknown
commands. It also traps other error types.

You can find the exception type for an error by inspecting the error object.
The following example shows how to get the full name of the exception for the
last error in a session:

```powershell
nonsenseString
$Error[0].Exception.GetType().FullName
```

```Output
nonsenseString: The term 'nonsenseString' is not recognized as a name of a
cmdlet, function, script file, or executable program. Check the spelling
of the name, or if a path was included, verify that the path is correct
and try again.
System.Management.Automation.CommandNotFoundException
```

You can have more than one `trap` statement in a script. Only one `trap`
statement can trap each error type. When a terminating error occurs, PowerShell
searches for the `trap` with the most specific match, starting in the current
Expand Down

0 comments on commit f2efd9e

Please sign in to comment.