使用PowerShell中的Trap方法处理终止错误

来源:互联网 发布:手机录像慢动作软件 编辑:程序博客网 时间:2024/06/06 02:59

今天我们要说的是用Trap关键字来处理终止错误,什么是Trap关键字,引用下官方的说明如下:

Trap 关键字指定要在发生终止错误时运行的语句列表。Trap 语句处理终止错误,并且允许继续执行脚本或函数,而不停止。

也就是说,平时我们遇到终止性错误的时候,脚本就不会继续执行了,而使用Trap语句处理终止错误可以继续执行脚本或者函数,看看如下例子:

function Test-Error{    Get-Content -Path D:\ -ErrorAction Stop    Get-Process}Test-Error


因为根本不存在D盘,所以我们得到了错误信息,这里要说明的是 Get-Content方法在这里并不能引发终止错误,所以我们用了一个参数配合那就是 -ErrorAction Stop。

Get-Content : Cannot find path 'D:\' because it does not exist.At line:3 char:5+     Get-Content -Path D:\ -ErrorAction Stop+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (D:\:String) [Get-Content], ItemNotFoundException    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand


但是当我们使用Trap关键字创建一个错误语句块处理,结果就会不一样。

function Test-Error{    trap [System.Exception]     {        "An error trapped"    }    Get-Content -Path D:\ -ErrorAction Stop        Get-Process}Test-Error

我们可以看到即使报错了,依然会继续运行后面的命令或者函数比如这里我们所写的Get-Process cmdlet,也就验证了一开始所说的使用Trap语句处理终止错误可以继续执行脚本或者函数。
An error trappedGet-Content : Cannot find path 'D:\' because it does not exist.At line:9 char:5+     Get-Content -Path D:\ -ErrorAction Stop+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (D:\:String) [Get-Content], ItemNotFoundException    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                          -------  ------    -----      ----- -----   ------     -- -----------                                                                                                                              113      20    12408      16724 ...68     0.20   2756 calc                                                                                                                                      54       9     1068       4620 ...17     0.03   1672 conhost                                                                                                                                   42       7      792       3248 ...09     0.00   2224 conhost                                                                                                                                   78       8     2320      18288 ...31     1.84   2996 conhost                                                                                                                                  255      11     1728       3796    47     0.42    320 csrss                                                                                                                                    325      25     2152      80048   235     9.72    388 csrss                                                                                                                                    196      13     3256      10692 ...01     0.08   2032 dllhost                                                                                                                                  301      50    96952     178808 ...80    12.59    688 dwm                                                                                                                                     1427     116    50424     131508 ...97    38.72   2268 explorer                                                                                                                                   0       0        0          4     0               0 Idle                                                                                                                                     666      55    33144      62168   303     4.36   1640 iexplore             

使用Break关键字终止运行

我们可以使用Break关键字来做到,即使触发了Trap异常处理后依然真正终止,而不会继续运行后续的方法和命令,如下:

function Test-Error{    trap [System.Exception]     {        "An error trapped"        break    }    Get-Content -Path D:\ -ErrorAction Stop        Get-Process}Test-Error

输出结果可以发现并没有继续运行后续的Get-Process命令:

An error trappedGet-Content : Cannot find path 'D:\' because it does not exist.At line:10 char:5+     Get-Content -Path D:\ -ErrorAction Stop+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (D:\:String) [Get-Content], ItemNotFoundException    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand


使用Continue关键字继续运行

使用Continue关键字,可以使Trap在处理终止错误后继续运行后续方法和命令,但是这里与没有加continue关键字有稍许不同的是,PowerShell并不会把错误写入错误流,所以你无法看到具体是什么报错,只有基本Trap的错误处理。

An error trappedHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                          -------  ------    -----      ----- -----   ------     -- -----------                                                                                                                              113      20    12408      16724 ...68     0.20   2756 calc                                                                                                                                      54       9     1068       4620 ...17     0.03   1672 conhost                                                                                                                                   42       7      792       3248 ...09     0.00   2224 conhost                                                                                                                                   78       8     2320      18288 ...31     1.84   2996 conhost                                                                                                                                  255      11     1728       3796    47     0.42    320 csrss                                                                                                                                    325      25     2152      80116   236     9.94    388 csrss                                                                                                                                    196      13     3256      10692 ...01     0.08   2032 dllhost                                                                                                                                  303      50    96716     178696 ...80    12.69    688 dwm       

最后要说明的是不同的Trap语句块放置范围会影响处理的层级也是不同的,这和方法的作用域类似。
0 0
原创粉丝点击