0
votes

I have a problem with my PowerShell code. Sometimes -for whatever reason- my script jumps right into the finally block wihtout any error message from the catch block. This is my PowerShell Code:

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
}

As you see I'm writing some log files in order to see what happened. I'm always getting right int values in my $sleepTime variable. However sometimes it jumps right into the finally block without writing "sleeptime over.." or even "chaught exception..."

1

1 Answers

0
votes

The only time the catch block will execute is when there's an exception in the try block. The only thing you're doing in the try block that has a chance of producing an exception is writing to the log file.

So the catch block will only be executed when there is an exception that prevents the output from being written to the log file.

Let me know if I'm missing anything.