diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md index 9ff318eeed44..187b3fe4e6a8 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md @@ -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 diff --git a/reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md b/reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md index 7bb1fb5506dd..935fd4b67f7e 100644 --- a/reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md @@ -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 diff --git a/reference/7.3/Microsoft.PowerShell.Core/About/about_Trap.md b/reference/7.3/Microsoft.PowerShell.Core/About/about_Trap.md index dfd70d0c0263..bd7731e64354 100644 --- a/reference/7.3/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/7.3/Microsoft.PowerShell.Core/About/about_Trap.md @@ -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 diff --git a/reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md b/reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md index 48a893ad79a9..3f0af34b3433 100644 --- a/reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md @@ -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