# if else in combo with try catch

**URL:** <https://forums.powershell.org/t/if-else-in-combo-with-try-catch/3255>\
**Category:** PowerShell Help\
**Created:** [October 15, 2014, 5:13am UTC](https://forums.powershell.org/t/if-else-in-combo-with-try-catch/3255 "2014-10-15T05:13:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael-glenn](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@michael-glenn](https://forums.powershell.org/u/michael-glenn)\
**Post date:** [October 15, 2014, 5:13am UTC](https://forums.powershell.org/t/if-else-in-combo-with-try-catch/3255/1 "2014-10-15T05:13:30Z")

</div>

im looking for examples of if else statements combined with try catch statements. im not finding much in my google searches  
is such a thing legal?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 15, 2014, 5:16am UTC](https://forums.powershell.org/t/if-else-in-combo-with-try-catch/3255/2 "2014-10-15T05:16:15Z")

</div>

Certainly. Both of those constructs can contain any other language elements inside the curly braces. For example:

```
try
{
    if ($something) {
        Do-Something
    }
    else
        Do-SomethingElse
    }
}
catch
{
    # Handle error
}

# Or:

if ($something)
{
    try
    {
        Do-Something
    }
    catch
    {
        # Handle error
    }
}
else
{
    try
    {
        Do-SomethingElse
    }
    catch
    {
        # Handle error
    }
}
```

---

<div class="post-metadata">

**Author:** ![michael-glenn](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@michael-glenn](https://forums.powershell.org/u/michael-glenn)\
**Post date:** [October 15, 2014, 5:26am UTC](https://forums.powershell.org/t/if-else-in-combo-with-try-catch/3255/3 "2014-10-15T05:26:13Z")

</div>

thank you…exactly what im looking for.

---

<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:47pm UTC](https://forums.powershell.org/t/if-else-in-combo-with-try-catch/3255/4 "2024-05-16T20:47:23Z")

</div>

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


