if else in combo with try catch

Welcome Forums General PowerShell Q&A if else in combo with try catch

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years ago.

  • Author
    Posts
  • #19717

    Participant
    Points: 0
    Rank: Member

    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?

  • #19718

    Member
    Points: 0
    Rank: Member

    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
        }
    }
    
  • #19719

    Participant
    Points: 0
    Rank: Member

    thank you...exactly what im looking for.

The topic ‘if else in combo with try catch’ is closed to new replies.