PowerShell中的错误与异常

PowerShell中无论是执行脚本还是命令时我们多少都会遇到失败和报错,今天我们就要说下关于PowerShell中的错误与异常。

先来说下错误,在PowerShell中发生运行错误时默认会以红字信息显示在屏幕上。

异常是一种来自错误的特别对象,对于错误而言你是得到一个错误,而异常可以让你做出一些处理。在PowerShell中通常会遇到两种错误类型,terminating终止错误与nonterminating非终止错误。

终止错误就代表PowerShell已经无法继续运行完全失败了,而如果是非终止错误则可以理解为运行中遇到错误,但是并不影响整个工作执行,即便遇到错误依然可以继续执行下去。

PowerShell中我们有三种错误设置选项,当遇到非终止错误的时候:

  • Inquire:要求用户介入操作
  • Continue:告诉用户发生错误,但是依然继续执行,此项为默认设置。
  • SilentlyContinue:类似于Continue的设置,但不同点是虽然继续执行但是不会在屏幕上显示错误信息。

下面这个例子我们举例说明如何使用ErrorAction参数针对使用不同的错误设置选项达到效果,使用Continue参数值我们可以告诉PowerShell即便出现错误也可以继续运行。

PS C:\Users\Administrator> Get-Content C:\a.txt -ErrorAction Continue
Get-Content : Cannot find path 'C:\a.txt' because it does not exist.
At line:1 char:1
+ Get-Content C:\a.txt -ErrorAction Continue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\a.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

而如果使用SilentlyContinuePowerShell则不会出现任何错误。

PS C:\Users\Administrator> Get-Content C:\a.txt -ErrorAction SilentlyContinue
PS C:\Users\Administrator>

当然在使用SilentlyContinue的前提是,此错误必须是非终止性错误,如果是一个终止性错误依然会提示:

PS C:\Users\Administrator> Get-ContentA C:\a.txt -ErrorAction SilentlyContinue
Get-ContentA : The term 'Get-ContentA' is not recognized as the name of a cmdlet, function, script file, or operable pr
ogram. 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
+ Get-ContentA C:\a.txt -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-ContentA:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

接下来我们再来说说如果做所谓的异常处理,这里就要提到异常捕获,异常捕获是我们在处理异常中最常用的办法,Try..Catch..Finally结构:

Try语句部分用来捕获可能发生的异常,我们把容易发生异常的执行代码放在Try语句块中。

Catch语句块是当Try捕获异常后,执行所对应的错误方案。

Finally语句块内的代码无论是否捕获到异常都会被执行。

我们先来看看如下例子:

Try
{
    Get-Content -Path C:\a.txt
}
Catch
{
    Write-Host "The path cannot found!"
}
Finally
{
    Write-Host "Please reinput a correct path."
}

为什么没有捕获异常成功,依然出现错误提示?

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

  

因为,Try语句块只能捕捉终止性错误,而如上代码不是一个终止性错误,为了能够是Try语句块捕捉到异常错误,我们必须用ErrorAction参数设置一个Stop参数值来暂停命令,如下:

PS C:\Windows\system32> 
Try
{
    Get-Content -Path C:\a.txt -ErrorAction Stop
}
Catch
{
    Write-Host "The path cannot found!"
}
Finally
{
    Write-Host "Please reinput a correct path."
}
The path cannot found!
Please reinput a correct path.
这样就能顺利捕捉到异常了。

最后再提一点,还有少部分人可能会用Trap的方式去处理错误,但由于用Trap处理错误容易使代码混乱,所以这里不是十分建议大家使用,比如要在错误发生前定义好Trap语句块:

Trap
{
    Write-Host "Cannot find path."
    Continue
}

Get-Content -Path C:\a.txt -ErrorAction Stop




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值