568数据 568数据


PowerShell: Try...Catch...Finally 实现方法

网络编程 PowerShell: Try...Catch...Finally 实现方法 06-21


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..."
}

linux下非阻塞模式网络通讯模型示例分享
#includestdio.h#includestring.h#includeunistd.h#includeerrno.h#includeunistd.h#includestdlib.h#includesysexits.h#includetime.h#includesys/time.h#includesys/types.h#includesys/uio.h#includesys/ioctl.h#

PowerShell 获取系统信息的函数
functionGet-SystemInfo{param($ComputerName=$env:COMPUTERNAME)$header='Hostname','OSName','OSVersion','OSManufacturer','OSConfiguration','OSBuildType','RegisteredOwner','RegisteredOrganization','Produc

PowerShell快速创建一个指定大小文件的实例分享
有时做性能测试时,需要准备几个大文件,内容不限制。我以前比较傻,是这样做,拼命地往一个文本文件中写字符串,要写2G,也得花很长时间。今


编辑:568数据

标签:示例,几个,是这样,也得,网络通讯