# Finally, except if not!

**URL:** <https://forums.powershell.org/t/finally-except-if-not/21186>\
**Category:** PowerShell Help\
**Created:** [December 18, 2022, 10:09pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186 "2022-12-18T22:09:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [December 18, 2022, 10:09pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/1 "2022-12-18T22:09:41Z")

</div>

MS docs say `finally` will always run even if you use exit keyword or press `CTRL + C` on keyboard to terminate script.

Seems like it’s not imune to return keyword:

```powershell
try {
    throw 1
}
catch
{
    return "x"
} finaly { "fin" }

```

Output:

> x

Expected:

> x  
> fin

> **[about Try Catch Finally - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7.4&viewFallbackFrom=powershell-7.3)**
>
> Describes how to use the \`try\`, \`catch\`, and \`finally\` blocks to handle terminating errors.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 18, 2022, 10:17pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/2 "2022-12-18T22:17:15Z")

</div>

> [@metablaster](#):
>
> Seems like it’s not imune to return keyword:

The documentation does not claim the `finally` block to be imune though.

> [@metablaster](#):
>
> `} finaly { "fin" }`

And BTW: it’s `finally` with a double `L` !!! 😉

---

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [December 18, 2022, 10:35pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/3 "2022-12-18T22:35:24Z")

</div>

> [@Olaf](#):
>
> And BTW: it’s `finally` with a double `L` !!!

Now that’s funny, it works as expected when correctly spelled to “finally”.

But why the heck it didn’t error out for mispelling a language keyword? 🙃

> [@Olaf](#):
>
> The documentation does not claim the `finally` block to be imune though.

Yes it does, it says `finally` will run no matter what.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 18, 2022, 10:57pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/4 "2022-12-18T22:57:19Z")

</div>

> [@metablaster](#):
>
> Yes it does, it says `finally` will run no matter what.

I’d read that diffently …

> **Freeing resources using finally**
> 
> To free resources used by a script, add a `finally` block after the `try` and `catch` blocks. The `finally` block statements run regardless of whether the `try` block encounters a terminating error. PowerShell runs the `finally` block before the script terminates or before the current block goes out of scope.
> 
> A `finally` block runs even if you use CTRL+C to stop the script. A `finally` block also runs if an Exit keyword stops the script from within a `catch` block.

It does not mention `return` at all.

---

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [December 18, 2022, 11:02pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/5 "2022-12-18T23:02:30Z")

</div>

> [@Olaf](#):
>
> It does not mention `return` at all.

You read it as if `exit` keyword is less impactful than `return` 😉  
Obviously there is no difference when it comes to how `finally` works.

Can you give an example where `finally` won’t run?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 18, 2022, 11:31pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/6 "2022-12-18T23:31:07Z")

</div>

> [@metablaster](#):
>
> Obviously there is no difference when it comes to how `finally` works.
> 
> Can you give an example where `finally` won’t run?

You claimed that it will not run when `return` is called. I just said that the documentation does not mention `return` explicitly … that’s all. 🤷🏽‍♂️

> [@metablaster](#):
>
> You read it as if `exit` keyword is less impactful than `return`

How would you read it?

**exit:**

> **[about Language Keywords - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_keywords?view=powershell-7.3#exit)**
>
> Describes the keywords in the PowerShell scripting language.

return:

> **[about Return - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return?view=powershell-7.3)**
>
> Exits the current scope, which can be a function, script, or script block.

---

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [December 19, 2022, 6:31am UTC](https://forums.powershell.org/t/finally-except-if-not/21186/7 "2022-12-19T06:31:40Z")

</div>

> [@Olaf](#):
>
> You claimed that it will not run when `return` is called. I just said that the documentation does not mention `return` explicitly … that’s all.

that’s fine, I didn’t notice my `finally` was misspelled because there was no error but it should have errored IMO.

I guess it was interpreted as function name but it wasn’t called due to `return` preceding…

> [@Olaf](#):
>
> How would you read it?

I read it as whole or what the docs are saying in whole rather than explicitly.  
similar to how the bible is read 🙂

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [December 19, 2022, 9:04am UTC](https://forums.powershell.org/t/finally-except-if-not/21186/8 "2022-12-19T09:04:04Z")

</div>

Return exits immediately so maybe that’s why I’d didn’t error. But I’d also think it would catch that error at parsing (or while you’re editing it)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [July 2, 2024, 2:39pm UTC](https://forums.powershell.org/t/finally-except-if-not/21186/9 "2024-07-02T14:39:19Z")

</div>

Closed on July 2, 2024, 2:39pm UTC


