# Error does not stop script from running

**URL:** <https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921>\
**Category:** PowerShell Help\
**Created:** [June 24, 2021, 11:58am UTC](https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921 "2021-06-24T11:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shysnail](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@shysnail](https://forums.powershell.org/u/shysnail)\
**Post date:** [June 24, 2021, 11:58am UTC](https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921/1 "2021-06-24T11:58:48Z")

</div>

In this simple code the script does not stop upon encountering error at the `catch` block. I want to export a log after a script has been run.

What am I doing wrong?

```auto
try
{
    $items = Import-Csv -Path "C:\WRONG PATH" -ErrorAction Stop
    $output = foreach ($item in $items)
    {
       $item.Name
       $item.Description
    }
       $output
}
catch
{
  Write-Warning $Error[0]
}

Write-Output "Successfully Run"

```

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/c/c9064228c46d42ddcf0c4b5640578aca1e0a61d2.png)

---

<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:** [June 24, 2021, 1:11pm UTC](https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921/2 "2021-06-24T13:11:47Z")

</div>

> [@shysnail](#):
>
> What am I doing wrong?

I’d say “Nothing”. PowerShell does exactly what’s expected. If there is a terminating error in the try block it will trigger the catch block. There you specified to output the error as warning. Then you explicitly write to the default ouput “Successfully Run”.

---

<div class="post-metadata">

**Author:** ![shysnail](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@shysnail](https://forums.powershell.org/u/shysnail)\
**Post date:** [June 25, 2021, 4:00am UTC](https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921/3 "2021-06-25T04:00:48Z")

</div>

oh, I was expecting Error Action = Stop to stop the rest of the script from running when there is any kind of error (terminating or non-terminating).

---

<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:** [June 25, 2021, 7:31am UTC](https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921/4 "2021-06-25T07:31:28Z")

</div>

If you would not have any error handling that would be the case. But for that reason we use error handling. To avoid to confront the user with an unhandled error. 😉

Just remove the try catch block and you will see. 😉

> [@shysnail](#):
>
> (terminating or non-terminating)

BTW: only with a terminating error the script would stop. A non-terminating error would not terminate the script execution. 😉

---

<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:** [May 16, 2024, 8:24pm UTC](https://forums.powershell.org/t/error-does-not-stop-script-from-running/16921/5 "2024-05-16T20:24:00Z")

</div>

Closed on May 16, 2024, 8:24pm UTC


