Powershell: need example for try-catch-finally where finally is a must RRS feed

  • General discussion

  • Hi, I need an example that use try-catch-finally clause where the -finally is **NECESSARY** vs try-catch clause where -finally is optional.  The example below only demonstrated that -finally is optional because with or without it, it won't make any different to my output.

    My Example, Note: $ErrorActionPreference is continue
        try {
            $value = 5 / 0
            write-output "illegal operation"
        $t = Get-Date
        Write-output ("operation is done at " + "$t")

    The reason is I need to know where -finally clause become necessary vs just put -finally clause no matter what.

    Friday, January 13, 2017 9:46 PM

All replies

  • The purpose of the Finally block is to run code even if the Try block encountered a terminating error, which would otherwise abort the script. If you use $ErrorActionPreference continue, then perhaps there is no need for a Finally block, since execution will always continue (I assume).

    Edit: Reference:

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, January 13, 2017 9:59 PM
  • assume you open a file in the "Try" block and then an error occurs.  You can close the file in the "Finally" block.

    There are many times when we want to always execute code even when there is no error.


    Friday, January 13, 2017 10:00 PM
  • Hi Richard Mueller,

    Continue is the default value for $ErrorActionPreference. 

    (5 / 0), this is a terminating error because the catch clause caught the error.

    I still don't understand where there is finally clause because without it, my script will continue.


    Friday, January 13, 2017 10:18 PM
  • Jrv, please provide an example (with actual script) that demonstrated the finally clause is necessary. Without finally clause, the script will stop and won't execute remaining codes.



    Friday, January 13, 2017 10:21 PM
  • Without the Finally the script will not stop.

    Suppose you want to do some work in Try.  If there is an error you want to continue but clean up the try effect.  If the code succeeds then ther is no need to do anything but continue - no clean up is necessary.

    If you search you will find hundreds of examples in C#, VB.Net, JavaScript, etc..

    To use Try/Catch you must use ErrorAction Stop and only that.

    If you don't have a use for Finally then just ignore it until you have code that requires it.  It is an advanced technique that is seldom used in PowerShell.


    Friday, January 13, 2017 10:29 PM
  • I have not experimented with -ErrorActionPreference Continue, but if you are correct that it continues after terminating errors, then you are correct that a Finally clause is not needed. But of course, the rest of your script may not make sense if the terminating error results in something being Null that you later expect to use.

    Without the preference, a terminating error stops the script, but first the Finally clause runs to clean things up. This could be closing files or gracefully closing Excel, for example, so things aren't hanging in memory.

    I don't recall seeing the Finally clause used, and I have not used it myself. But then, I don't recall getting terminating errors. I generally use Try clauses to trap things I expect could happen, then use break to exit and abort the script, as it may make little sense to continue.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, January 13, 2017 10:30 PM
  • Hi, I need an example that use try-catch-finally clause where the -finally is **NECESSARY**


    -- Bill Stewart [Bill_Stewart]

    Saturday, January 14, 2017 3:21 AM