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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:55:54+00:00 2026-05-24T23:55:54+00:00

For some reason my script is not catching the exception when I run the

  • 0

For some reason my script is not catching the exception when I run the following script against a file that does not exist. I based this code from examples I found on the web but it doesn’t seem to work for me.

I would appreciate any tips or pointers on how to fix this.

Note: in the example below I’ve also tried

trap [Exception] {

but that didn’t work either.

Here’s the script:

function CheckFile($f) {

      trap {
        write-host "file not found, skipping".
        continue
      }

      $modtime = (Get-ItemProperty $f).LastWriteTime

      write-host "if file not found then shouldn't see this"
}


write-host "checking a file that does not exist"
CheckFile("C:\NotAFile")
write-host "done."

output:

PS > .\testexception.ps1
checking a file that does not exist
Get-ItemProperty : Cannot find path 'C:\NotAFile' because it does not exist.
At C:\Users\dleclair\Documents\Visual Studio 2010\lib\testexception.ps1:12 char:35
+       $modtime = (Get-ItemProperty <<<<  $f).LastWriteTime
    + CategoryInfo          : ObjectNotFound: (C:\NotAFile:String) [Get-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand

if file not found then shouldn't see this
done.
PS >
  • 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-24T23:55:55+00:00Added an answer on May 24, 2026 at 11:55 pm

    Try like this:

    trap { write-host "file not found, skipping";continue;}
    $modtime = Get-ItemProperty c:\manoj -erroraction stop
    

    Based on comments from OP:

    I think you misunderstood what is being said in the article you have linked to:

    In this example, we used continue to caused execution to return to the
    scope the trap is in and execute the next command. It’s important to
    note that execution only returns to the scope of the trap, so if the
    exception was thrown inside a function, or even inside a if statement,
    and trapped outside of it … the continue will pick up at the end of
    the nested scope.

    So if you do something like this:

    trap{ write-host $_; continue;}
    throw "blah"
    write-host after
    

    after will be printed.

    But if you do something like this:

    trap{ write-host $_ ; continue}
    function fun($f) {
    
    
          throw "blah"
          write-host after
    }
    
    fun
    write-host "outside after"
    

    after will NOT be printed, but outside after will be.

    Alternatively, use a try-catch block:

          try{
          $modtime = (Get-ItemProperty $f -erroraction stop).LastWriteTime
          write-host "if file not found then shouldn't see this"
          }
          catch{
            write-host "file not found, skipping".
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, Section 1 works but Section 2 does not. When run in
For some reason the following code does not work as expected when in IE
Ok for some reason my script does not return any data from a JSON
For some reason, the following code will work: The following code does not work:
For some reason the script below is not working. This is the code I
I created a script that resizes images based on their ratio. For some reason,
I have this login script, but for some reason its not working. I checked
For some reason, my PHP mail is not sending. This is my script: $tom
For some reason This php script won't echo anything: <?php setcookie(pop,'hi',time()+604800); echo $HTTP_COOKIE_VARS['pop']; ?>
For some reason, we have a script that creates batch files to XCOPY our

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.