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

  • Home
  • SEARCH
  • 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 7921607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:38:43+00:00 2026-06-03T16:38:43+00:00

Consider the following snippet: function fail { throw simulated failure } fail When running

  • 0

Consider the following snippet:

function fail {
    throw "simulated failure"
}
fail

When running the script, the default exception handling prints the line and command where the exception was thrown:

simulated failure
At D:\tmp\Untitled1.ps1:2 char:10
+     throw <<<<  "simulated failure"
    + CategoryInfo          : OperationStopped: (simulated failure:String) [],     RuntimeException
    + FullyQualifiedErrorId : simulated failure

On the other hand, if I catch the exception and print it myself:

function fail {
    throw "simulated failure"
}
try {
    fail
} catch {
    Write-Error $_
    exit 1
}

the output of Write-Error only tells me that the error happened inside the script:

D:\tmp\Untitled2.ps1 : simulated failure
At line:1 char:16
+ .\Untitled2.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Untitled2.ps1

How can I achieve the same output as in the first case?

NOTE: The reason I want to catch the exception is to do “exit 1”. By default powershell exits with 0 even after exceptions, so the script appears to have been successful.

  • 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-06-03T16:38:45+00:00Added an answer on June 3, 2026 at 4:38 pm

    It turns out this was trivial. I shouldn’t use Write-Error, but just output the exception directly:

    function fail {
        throw "simulated failure"
    }
    try {
        fail
    } catch {
        $_
        exit 1
    }
    

    and the output is:

    simulated failure
    At D:\tmp\Untitled2.ps1:2 char:14
    +         throw <<<<  "simulated failure"
        + CategoryInfo          : OperationStopped: (simulated failure:String) [], RuntimeException
        + FullyQualifiedErrorId : simulated failure
    

    UPDATE:

    This approach obviously writes to output stream. If you want to write to the error stream instead (as Write-Error does) you are probably out of luck, according to this post: How do I write to standard error in PowerShell?. Write-Error cannot be used to write a specific string (it adds its own stuff) and there’s no equivalent of Write-Error that deals nicely with redirection. I would personally love to see an Out-Error cmdlet that mimics Out-Default but writes to the error stream of the current pipeline element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
In the following snippet consider replacing line 8 with commented equivalent 1. private static
Consider the following snippet of code: function parseXml(xml) { xmlObject= xml; alert(xmlObject.xml); } function
Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
Consider the following snippet: puts 'hello'.gsub(/.+/, '\0 \\0 \\\0 \\\\0') This prints ( as
Consider the following snippet running in an applet insdie a browser: WebServiceClient s =
Consider the following PHP snippet: <?php class Is { function __get($key) { $class =
Consider the following command line snippet: $ cd /tmp/ $ mkdir dirA $ mkdir
Consider the following code snippet namespace ConsoleApplication1 { public delegate TResult Function<in T, out
Consider the following snippet (error handling missing on purpose): void* foo(const char *path, off_t

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.