0

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

andreaspfr
  • 2,298
  • 5
  • 42
  • 51

1 Answers1

0

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.

Elijah W. Gagne
  • 2,801
  • 4
  • 31
  • 29
  • I think the point to his question was `sometimes it jumps right into the finally block without writing "sleeptime over.." or even "chaught exception..."`, meaning that his 'Finally' statement is written to the log, but neither `try` or `catch`, implying that because `try` didn't write, that `catch` should have, yet it did not. – SpellingD Aug 10 '12 at 21:36
  • @SpellingD That does make the question make more sense. In the example code provided I don't see how it's possible to skip the `try` block. I notice that there are variables `$reservationDuration` and `$prepareTime` that are never initialized with values so it appears that we're not looking at the full script. – Elijah W. Gagne Aug 11 '12 at 14:37
  • Sorry for the incomprehensible question. Well I did omit some of my code to make it more readable. There are some methods in front of the $sleepTime calculation. But I thought -because my first log-File write method "tinfo:`tstart sleep before warning VIP sleep-time:"works the code before is then all right. The variables you mentioned are just initalized before. Or could it be that there is an exception and the log-file is still written due to the $ErrorActionPreference = Continue attribute and later on the program jumps write into the finally statement? – andreaspfr Aug 13 '12 at 10:04
  • 1
    Yes, it looks like that's the problem. I found http://stackoverflow.com/questions/3097785 where it's explained that try/catch can only catch terminating errors. You're probably hitting a file-in-use error, which is a non-terminating error. I think your problem will be fixed by changing `out-file $logFilePath -append` to `out-file $logFilePath -append -ea stop`. – Elijah W. Gagne Aug 13 '12 at 12:40