11---
22description : Describes a keyword that handles a terminating error.
33Locale : en-US
4- ms.date : 07/05/2024
4+ ms.date : 01/13/2026
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about_Trap
@@ -28,10 +28,10 @@ following ways:
2828 the default.
2929
3030 > [ !NOTE]
31- > When the terminating error occurs in a subordinate script block, such as
31+ > When the terminating error occurs in a subordinate statement block, such as
3232 > an ` if ` statement or ` foreach ` loop, the statements in the ` trap ` block are
3333 > run and execution continues at the next statement outside the subordinate
34- > script block.
34+ > block.
3535
3636- Display the error and abort execution of the script or function containing
3737 the ` trap ` using ` break ` in the ` trap ` statement.
@@ -42,7 +42,7 @@ following ways:
4242The statement list of the ` trap ` can include multiple conditions or function
4343calls. A ` trap ` can write logs, test conditions, or even run another program.
4444
45- ### Syntax
45+ ## Syntax
4646
4747The ` trap ` statement has the following syntax:
4848
@@ -59,7 +59,7 @@ types of errors the `trap` catches.
5959A script or command can have multiple ` trap ` statements. ` trap ` statements can
6060appear anywhere in the script or command.
6161
62- ### Trapping all terminating errors
62+ ## Trapping all terminating errors
6363
6464When a terminating error occurs that isn't handled in another way in a script
6565or command, PowerShell checks for a ` trap ` statement that handles the error. If
@@ -103,7 +103,7 @@ At line:3 char:5
103103```
104104
105105The following example includes a ` trap ` statement that displays the error by
106- using the ` $_ ` automatic variable:
106+ using the ` $_ ` or ` $PSItem ` automatic variable:
107107
108108``` powershell
109109function TrapTest {
@@ -134,17 +134,16 @@ At line:3 char:5
134134```
135135
136136> [ !IMPORTANT]
137- > ` trap ` statements may be defined anywhere within a given script block , but
138- > always apply to all statements in that script block . At runtime, ` trap `
137+ > ` trap ` statements can be defined anywhere within a given scriptblock , but
138+ > always apply to all statements in that scriptblock . At runtime, ` trap `
139139> statements in a block are defined before any other statements are executed.
140- > In JavaScript, this is known as
141- > [ hoisting] ( https://wikipedia.org/wiki/JavaScript_syntax#hoisting ) . This means
142- > that ` trap ` statements apply to all statements in that block even if
140+ > In other languages, such as JavaScript, this is known as [ hoisting] [ 06 ] . This
141+ > means that ` trap ` statements apply to all statements in that block even if
143142> execution hasn't advanced past the point at which they're defined. For
144143> example, defining a ` trap ` at the end of a script and throwing an error in
145144> the first statement still triggers that ` trap ` .
146145
147- ### Trapping specific errors
146+ ## Trapping specific errors
148147
149148A script or command can have multiple ` trap ` statements. A ` trap ` can be
150149defined to handle specific errors.
@@ -201,7 +200,7 @@ System.Management.Automation.CommandNotFoundException
201200You can have more than one ` trap ` statement in a script. Only one ` trap `
202201statement can trap each error type. When a terminating error occurs, PowerShell
203202searches for the ` trap ` with the most specific match, starting in the current
204- script block of execution.
203+ scriptblock of execution.
205204
206205The following script example contains an error. The script includes a general
207206` trap ` statement that traps any terminating error and a specific ` trap `
@@ -262,13 +261,13 @@ The attempt to divide by zero doesn't create a **CommandNotFoundException**
262261error. The other ` trap ` statement, which traps any terminating error, traps the
263262divide by zero error.
264263
265- ### Trapping errors in a script block
264+ ## Trapping errors in a scriptblock
266265
267266By default, when a terminating error is thrown, execution transfers to the trap
268267statement. After the ` trap ` block is run, control returns to the next statement
269268block after the location of the error.
270269
271- For example, when a terminating error occurs in an ` foreach ` statement, the
270+ For example, when a terminating error occurs in a ` foreach ` statement, the
272271` trap ` statement is run and execution continues at the next statement after the
273272` foreach ` block, not within the ` foreach ` block.
274273
@@ -302,16 +301,16 @@ after loop
302301
303302In the output, you can see the loops continue until the last iteration. When
304303the script tries to divide 1 by 0, PowerShell throws a terminating error. The
305- script skips the rest of the ` foreach ` script block , runs the ` try ` statement,
306- and continues after the ` foreach ` script block .
304+ script skips the rest of the ` foreach ` statement , runs the ` try ` statement,
305+ and continues after the ` foreach ` statement .
307306
308- ### Trapping errors and scope
307+ ## Trapping errors and scope
309308
310- If a terminating error occurs in the same script block as the ` trap ` statement,
309+ If a terminating error occurs in the same scriptblock as the ` trap ` statement,
311310PowerShell runs the list of statements defined by the ` trap ` . Execution
312311continues at the statement after the error. If the ` trap ` statement is in a
313- different script block from the error, execution continues at the next
314- statement that's in the same script block as the ` trap ` statement.
312+ different scriptblock from the error, execution continues at the next
313+ statement that's in the same scriptblock as the ` trap ` statement.
315314
316315For example, if an error occurs in a function, and the ` trap ` statement is in
317316the function, the script continues at the next statement. The following script
@@ -387,7 +386,7 @@ back into the function after the `trap` statement runs.
387386
388387> [ !CAUTION]
389388> When multiple traps are defined for the same error condition, the first
390- > ` trap ` defined lexically (highest in the script block ) is used.
389+ > ` trap ` defined lexically (highest in the scriptblock ) is used.
391390
392391In the following example, only the ` trap ` with ` whoops 1 ` runs.
393392
@@ -402,7 +401,7 @@ trap { 'whoops 2'; continue }
402401> statement inside a function or dot sourced script, when the function or dot
403402> sourced script exits, all ` trap ` statements inside are removed.
404403
405- ### Using the break and continue keywords
404+ ## Using the break and continue keywords
406405
407406You can use the ` break ` and ` continue ` keywords in a ` trap ` statement to
408407determine whether a script or command continues to run after a terminating
@@ -481,11 +480,11 @@ statement runs. No error is written to the error stream.
481480## Notes
482481
483482` trap ` statements provide a way to ensure all terminating errors within a
484- script block are handled. For more finer -grained error handling, use
485- ` try ` / ` catch ` blocks where traps are defined using ` catch ` statements. The
483+ scriptblock are handled. For more fine -grained error handling, use
484+ ` try/ catch ` blocks where traps are defined using ` catch ` statements. The
486485` catch ` statements only apply to the code inside the associated ` try `
487486statement. For more information, see
488- [ about_Try_Catch_Finally] ( about_Try_Catch_Finally.md ) .
487+ [ about_Try_Catch_Finally] [ 05 ] .
489488
490489## See also
491490
0 commit comments