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 187b3fe4e6a..4abab85d5cf 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md @@ -1,7 +1,7 @@ --- description: Describes a keyword that handles a terminating error. Locale: en-US -ms.date: 01/17/2024 +ms.date: 07/05/2024 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-5.1&WT.mc_id=ps-gethelp schema: 2.0.0 title: about Trap @@ -274,20 +274,21 @@ For example, when a terminating error occurs in an `foreach` statement, the ```powershell trap { 'An error occurred!'} -foreach ($x in 3..0) { - 1/$x - 'after division' +foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" } 'after loop' ``` ```Output -0.333333333333333 -after division -0.5 -after division -1 -after division +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = An error occurred! Attempted to divide by zero. At line:3 char:4 @@ -447,24 +448,34 @@ The following sample function uses the `continue` keyword in a `trap` statement: ```powershell -function continue_example { +function ContinueExample { trap { 'Error trapped' continue } - 1/$null - 'Function completed.' + foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" + } + 'End of function' } -continue_example +ContinueExample ``` ```Output +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = Error trapped -Function completed. +End of function ``` -The function resumes after the error is trapped, and the `Function completed` +The function resumes after the error is trapped, and the `End of function` statement runs. No error is written to the error stream. ## Notes 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 935fd4b67f7..094a3f50f5f 100644 --- a/reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md @@ -1,7 +1,7 @@ --- description: Describes a keyword that handles a terminating error. Locale: en-US -ms.date: 01/17/2024 +ms.date: 07/05/2024 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7.2&WT.mc_id=ps-gethelp schema: 2.0.0 title: about Trap @@ -261,25 +261,26 @@ For example, when a terminating error occurs in an `foreach` statement, the ```powershell trap { 'An error occurred!'} -foreach ($x in 3..0) { - 1/$x - 'after division' +foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" } 'after loop' ``` ```Output -0.333333333333333 -after division -0.5 -after division -1 -after division +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = An error occurred! RuntimeException: Line | - 3 | 1/$x - | ~~~~ + 4 | "`t$(1/$x)" + | ~~~~ | Attempted to divide by zero. after loop ``` @@ -425,24 +426,34 @@ The following sample function uses the `continue` keyword in a `trap` statement: ```powershell -function continue_example { +function ContinueExample { trap { 'Error trapped' continue } - 1/$null - 'Function completed.' + foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" + } + 'End of function' } -continue_example +ContinueExample ``` ```Output +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = Error trapped -Function completed. +End of function ``` -The function resumes after the error is trapped, and the `Function completed` +The function resumes after the error is trapped, and the `End of function` statement runs. No error is written to the error stream. ## Notes 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 3f0af34b343..0b189b47495 100644 --- a/reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md @@ -1,7 +1,7 @@ --- description: Describes a keyword that handles a terminating error. Locale: en-US -ms.date: 01/17/2024 +ms.date: 07/05/2024 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7.4&WT.mc_id=ps-gethelp schema: 2.0.0 title: about Trap @@ -261,25 +261,26 @@ For example, when a terminating error occurs in an `foreach` statement, the ```powershell trap { 'An error occurred!'} -foreach ($x in 3..0) { - 1/$x - 'after division' +foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" } 'after loop' ``` ```Output -0.333333333333333 -after division -0.5 -after division -1 -after division +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = An error occurred! RuntimeException: Line | - 3 | 1/$x - | ~~~~ + 4 | "`t$(1/$x)" + | ~~~~ | Attempted to divide by zero. after loop ``` @@ -425,24 +426,34 @@ The following sample function uses the `continue` keyword in a `trap` statement: ```powershell -function continue_example { +function ContinueExample { trap { 'Error trapped' continue } - 1/$null - 'Function completed.' + foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" + } + 'End of function' } -continue_example +ContinueExample ``` ```Output +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = Error trapped -Function completed. +End of function ``` -The function resumes after the error is trapped, and the `Function completed` +The function resumes after the error is trapped, and the `End of function` statement runs. No error is written to the error stream. ## Notes diff --git a/reference/7.5/Microsoft.PowerShell.Core/About/about_Trap.md b/reference/7.5/Microsoft.PowerShell.Core/About/about_Trap.md index a31d24f5ae5..dd01bede950 100644 --- a/reference/7.5/Microsoft.PowerShell.Core/About/about_Trap.md +++ b/reference/7.5/Microsoft.PowerShell.Core/About/about_Trap.md @@ -1,7 +1,7 @@ --- description: Describes a keyword that handles a terminating error. Locale: en-US -ms.date: 01/17/2024 +ms.date: 07/05/2024 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7.5&WT.mc_id=ps-gethelp schema: 2.0.0 title: about Trap @@ -261,25 +261,26 @@ For example, when a terminating error occurs in an `foreach` statement, the ```powershell trap { 'An error occurred!'} -foreach ($x in 3..0) { - 1/$x - 'after division' +foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" } 'after loop' ``` ```Output -0.333333333333333 -after division -0.5 -after division -1 -after division +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = An error occurred! RuntimeException: Line | - 3 | 1/$x - | ~~~~ + 4 | "`t$(1/$x)" + | ~~~~ | Attempted to divide by zero. after loop ``` @@ -425,24 +426,34 @@ The following sample function uses the `continue` keyword in a `trap` statement: ```powershell -function continue_example { +function ContinueExample { trap { 'Error trapped' continue } - 1/$null - 'Function completed.' + foreach ($x in 3..-1) { + "1/$x = " + "`t$(1/$x)" + } + 'End of function' } -continue_example +ContinueExample ``` ```Output +1/3 = + 0.333333333333333 +1/2 = + 0.5 +1/1 = + 1 +1/0 = Error trapped -Function completed. +End of function ``` -The function resumes after the error is trapped, and the `Function completed` +The function resumes after the error is trapped, and the `End of function` statement runs. No error is written to the error stream. ## Notes