【发布时间】:2012-08-10 13:14:35
【问题描述】:
我的 PowerShell 代码有问题。有时——无论出于何种原因——我的脚本直接跳到 finally 块中,而没有来自 catch 块的任何错误消息。这是我的 PowerShell 代码:
try
{
$sleepTime = $reservationDuration - $prepareTime
(get-date -format hh:mm:ss)+"`tinfo:`tstart sleep before warning VIP sleep-time:"+$sleepTime | out-file $logFilePath -append
start-sleep -s $sleepTime
(get-date -format hh:mm:ss)+"`tinfo:`tsleeptime over" | out-file $logFilePath -append
}
catch
{
(get-date -format hh:mm:ss)+"`terror:`tchaught exception: "+$error[0] | out-file $logFilePath -append
}
finally{
(get-date -format hh:mm:ss)+"`tinfo:`tFinally" | out-file $logFilePath -append
}
如您所见,我正在编写一些日志文件以查看发生了什么。我总是在我的 $sleepTime 变量中得到正确的 int 值。但是有时它会直接跳到 finally 块中而没有写“sleeptime over..”甚至“chaught exception...”
【问题讨论】:
标签: powershell exception-handling try-catch-finally