Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples #11236

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 29 additions & 18 deletions reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
47 changes: 29 additions & 18 deletions reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
47 changes: 29 additions & 18 deletions reference/7.5/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down