添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more

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

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.)

So... my bad. :) It was not RuntimeException. I just copied over FullyQualifiedErrorId, and that does not contain detail important for that scenario. Regarding v3: I can't find any reference (yet). Doug Finke mentioned this calculation as example in his upcoming book, "PowerShell for Developers", so I tried and realized that in v3 it actually can be caught. Sorry for any confusion caused by using wrong terminology... :) – BartekB May 16 '12 at 18:29 Thx for the update. Should you find a reference, I hope you would still post it here. – Lieven Keersmaekers May 18 '12 at 8:13 OK, not sure why I haven't spotted this sooner... Look up WMF 3.0 Release Notes, Page 13. There is information about this particular case: "Constant folding is not performed during parsing." And later, in Error Message column "No parsing. The error is detected at runtime". Obviously, with that we get possibility to catch this error. You can find Release Notes for WMF 3.0 RC here: microsoft.com/en-us/download/details.aspx?id=29939 – BartekB Jun 4 '12 at 14:37

You can try to throw an exception with that kind of line : trap { "Your Exception" } 1/0
This will throw the exception "divide by 0". Though I don't really understand why your code doesn't throw an exception ._.
PS : Isn't that supposed to be catch [System.SystemException] ? :)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.