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 7953907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:16:04+00:00 2026-06-04T03:16:04+00:00

On my Machine each one of the following code snippets throws and exception instead

  • 0

On my Machine each one of the following code snippets throws and exception instead of printing to the standard output “1” and “2”
Why the exception is not being Caught?

try {
    [int]$a = 1/0
}
catch {
    write 1
}
finally {
    write 2
}

try {
    [int]$a = 1/0
}
catch [System.Exception] {
    write 1
}
finally {
    write 2
}
  • 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-04T03:16:05+00:00Added an answer on June 4, 2026 at 3:16 am

    As you are using constants, the interpreter tries to precompute the result and fails with a division by zero error. Your code does not even get executed so there’s nothing to trap.

    You can verify this for yourself by changing your code to use variables, forcing it to be executed.

    try {
        $divisor = 0
        [int]$a = 1/$divisor
    }
    catch {
        write 1
    }
    finally {
        write 2
    }
    

    From Windows PowerShell in Action (p.257)

    The example here uses 1/$null. The reason for doing this instead of
    simply 1/0 is because the PowerShell interpreter does something called
    constant expression folding.

    It looks at expressions that contain only constant values. When it
    sees one, it evaluates that expression once at compile time so it
    doesn’t have to waste time doing it again at runtime.

    This means that impossible expressions, such as division by zero, are
    caught and treated as parsing errors. Parsing errors can’t be caught
    and don’t get logged when they’re entered interactively, so they don’t
    make for a good example. (If one script calls another script and that
    script has one of these errors, the calling script can catch it, but
    the script being parsed cannot.)

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

Sidebar

Related Questions

My machine apparently won't draw vertex lists in pyglet. The following code renders two
my doubt is the following, I have a network with 5 pcs, each machine
Consider the following code: struct TrainingExample { array<double, N> input; array<double, M> output; };
The output of the following program on my machine with ATI Firepro V8750 is
I have a perl script (call it worker) installed on each node/machine (4 total)
A Java thread is always in one of the following ten states: NEW: Just
Well my problem is the following. I have a piece of code that runs
I have written a code snippet which i definitely feel its not written a
Possible Duplicate: Undefined Behavior and Sequence Points In C++ on a machine code level,
I love to organize my code, so ideally I want one class per file

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.