Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:48:01+00:00 2026-05-29T17:48:01+00:00

Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226 , I noticed this strange-to-me

  • 0

Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226, I noticed this strange-to-me contraption:

$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return

What is the reason for this as opposed to the following?

$URL_Format_Error = [string]"..."
Throw $URL_Format_Error

Or even better:

$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error

As I understand, you should use Write-Error for non-terminating errors, and Throw for terminating errors, so it seems to me that you should not use Write-Error followed by Return. Is there a difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:48:06+00:00Added an answer on May 29, 2026 at 5:48 pm

    Write-Error should be used if you want to inform the user of a non-critical error. By default all it does is print an error message in red text on the console. It does not stop a pipeline or a loop from continuing. Throw on the other hand produces what is called a terminating error. If you use throw, the pipeline and/or current loop will be terminated. In fact all execution will be terminated unless you use a trap or a try/catch structure to handle the terminating error.

    There is one thing to note, if you set $ErrorActionPreference to "Stop" and use Write-Error it will produce a terminating error.

    In the script you linked to we find this:

    if ($url.Contains("http")) {
           $request = [System.Net.HttpWebRequest]::Create($url)
    }
    else {
           $URL_Format_Error = [string]"Connection protocol not specified. Recommended action: Try again using protocol (for example 'http://" + $url + "') instead. Function aborting..."
           Write-Error $URL_Format_Error
        return
       }
    

    It looks like the author of that function wanted to stop the execution of that function and display an error message on screen but did not want the entire script to stop executing. The script author could have used throw however it would mean you would have to use a try/catch when calling the function.

    return will exit the current scope which can be a function, script, or script block. This is best illustrated with code:

    # A foreach loop.
    foreach ( $i in  (1..10) ) { Write-Host $i ; if ($i -eq 5) { return } }
    
    # A for loop.
    for ($i = 1; $i -le 10; $i++) { Write-Host $i ; if ($i -eq 5) { return } }
    

    Output for both:

    1
    2
    3
    4
    5
    

    One gotcha here is using return with ForEach-Object. It will not break processing like one might expect.

    More information:

    • $ErrorActionPreference: about_Preference_Variables
    • try/catch: about_Try_Catch_Finally
    • trap: about_Trap
    • throw: about_Throw
    • return: about_Return
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to get the value of this input if it changes. I have
i'm looking to get a BackColor of Label in ASP.NET Form and Change this
I'm looking to get the showGUI() method work, the compiler says this is not
I am looking to get http://www.mysite.com/app_directory in the model. Note my app is not
I'm looking to get a Java string to ' WlanHostedNetworkSetSecondaryKey ' and ' WlanHostedNetworkSetProperty
I am looking get to grips with functional programming concepts. I've used Javascript for
Looking to get a list of all the files (commit comments would be nice
Was looking to get peoples thoughts on keeping a Lucene index up to date
When looking to get data from a web page whats the recommended method if
I'm looking to get the result of a command as a variable in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.