手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >PowerShell: Try...Catch...Finally 实现方法
PowerShell: Try...Catch...Finally 实现方法
摘要:复制代码代码如下:functionTry{param([ScriptBlock]$Command=$(throw"Theparameter-...

复制代码 代码如下:

function Try

{

param

(

[ScriptBlock]$Command = $(throw "The parameter -Command is required."),

[ScriptBlock]$Catch = { throw $_ },

[ScriptBlock]$Finally = {}

)

& {

$local:ErrorActionPreference = "SilentlyContinue"

trap

{

trap

{

& {

trap { throw $_ }

&$Finally

}

throw $_

}

$_ | & { &$Catch }

}

&$Command

}

& {

trap { throw $_ }

&$Finally

}

}

使用示例:

复制代码 代码如下:

# Example usage

Try {

echo " ::Do some work..."

echo " ::Try divide by zero: $(0/0)"

} -Catch {

echo " ::Cannot handle the error (will rethrow): $_"

#throw $_

} -Finally {

echo " ::Cleanup resources..."

}

【PowerShell: Try...Catch...Finally 实现方法】相关文章:

PowerShell Out-File向只读文件写入内容的方法

PowerShell读取文本文件指定行内容的方法

Windows Powershell 执行文件和脚本

PowerShell函数参数用星号隐藏的方法

PowerShell中使用正则表达式跨行匹配字符串的方法

PowerShell中字符串分行显示的两种方法技巧

PowerShell获取字符串长度的方法

PowerShell把文件删除到回收站的方法

PowerShell中使用GetType获取变量数据类型

PowerShell中按修改时间查找文件的方法

精品推荐
分类导航