Windows Vista Forums

why cannot Trap catch error

  1. #1


    AdityaKir Guest

    why cannot Trap catch error

    hi

    I have below statement in my code but for some reason the trap is not
    catching the error. Any reason why ??

    function CheckFolder()
    {
    trap
    {
    write-Host "[ Error ] " + $_ -foreground green
    continue
    }

    test-Path "E:\Scripts\W:"


    }
    Greet



      My System SpecsSystem Spec

  2. #2


    Vadims Podans Guest

    Re: why cannot Trap catch error

    but you haven't error. Test-Path will never generate error and allways will
    return True or False if only path not contains semicolon character in middle
    (if semicolon is the last character, error will not generated and if
    semicolon is the first character you will be promted for path). Check this:
    [vPodans] test-path sdlkfvdsaflkdsfalkdsfasdkflwmdafa
    False
    [vPodans] test-path sdkjsad;
    False
    [vPodans] test-path sdkj;sad
    False
    The term 'sad' is not recognized as a cmdlet, function, operable program, or
    script file. Verify the term and try again
    ..
    At line:1 char:19
    + test-path sdkj;sad <<<<
    + CategoryInfo : ObjectNotFound: (sad:String) [],
    CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    [vPodans] test-path ;sdkjsad

    cmdlet Test-Path at command pipeline position 1
    Supply values for the following parameters:
    Path[0]:

    so, if semicolon character is in path middle, then your Trap will works.
    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "AdityaKir" <AdityaKir@xxxxxx> rakstīja ziņojumā
    "news1080853-9503-4252-A251-E2AD9596DDE7@xxxxxx"...

    > hi
    >
    > I have below statement in my code but for some reason the trap is not
    > catching the error. Any reason why ??
    >
    > function CheckFolder()
    > {
    > trap
    > {
    > write-Host "[ Error ] " + $_ -foreground green
    > continue
    > }
    >
    > test-Path "E:\Scripts\W:"
    >
    >
    > }
    > Greet

      My System SpecsSystem Spec

  3. #3


    Vadims Podans Guest

    Re: why cannot Trap catch error

    also error will be generated if path contains space. But if path is enclosed
    in quotes, then any character inside will be accepted and True or False will
    returned.

    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "AdityaKir" <AdityaKir@xxxxxx> rakstīja ziņojumā
    "news1080853-9503-4252-A251-E2AD9596DDE7@xxxxxx"...

    > hi
    >
    > I have below statement in my code but for some reason the trap is not
    > catching the error. Any reason why ??
    >
    > function CheckFolder()
    > {
    > trap
    > {
    > write-Host "[ Error ] " + $_ -foreground green
    > continue
    > }
    >
    > test-Path "E:\Scripts\W:"
    >
    >
    > }
    > Greet

      My System SpecsSystem Spec

  4. #4


    RickB Guest

    Re: why cannot Trap catch error

    Apparently your default error action is to continue.
    Try this.

    function CheckFolder($name){
    trap {
    write-Host "[ Error ] " + $_ -foreground green
    continue
    }
    test-Path $name -ErrorAction stop
    }


    AdityaKir wrote:

    > hi
    >
    > I have below statement in my code but for some reason the trap is not
    > catching the error. Any reason why ??
    >
    > function CheckFolder()
    > {
    > trap
    > {
    > write-Host "[ Error ] " + $_ -foreground green
    > continue
    > }
    >
    > test-Path "E:\Scripts\W:"
    >
    >
    > }
    > Greet

      My System SpecsSystem Spec

  5. #5


    ajax76 Guest

    Re: why cannot Trap catch error

    Test-Path cmdlet generates an exception [RuntimeException] if the Path
    parameter is not in a supported format.
    In all over cases returned [Boolean], depending on whether specified
    path is exist.
    And, of course, you must set $ErrorActionPreference in Stop or
    Inquire.
    Try this variant:

    function foo()
    {
    param($name)

    try
    {
    if( Test-Path $name -ErrorAction 1 ){ $TRUE }
    else{ throw( New-Object IO.DirectoryNotFoundException ) }
    }
    catch [Management.Automation.RuntimeException],
    [IO.DirectoryNotFoundException]
    {
    $_.exception.message
    }
    }

      My System SpecsSystem Spec

why cannot Trap catch error

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trap an Error from an EXE OldDog PowerShell 4 05 Aug 2009
80005000 Error trap Dave Susemiehl VB Script 1 20 Feb 2009
Ping error trap? Jeroen PowerShell 10 11 Feb 2009
How to get more information about the error from trap statement AdityaKir PowerShell 1 04 Dec 2008
How to trap this error in PowerShell =?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?= PowerShell 1 10 Sep 2006