Windows Vista Forums

syntax question on the throw statement
  1. #1


    Flea Guest

    syntax question on the throw statement

    I was wondering if anyone could explain the syntaxt of the throw statement?
    For example, I have the following script:

    param
    (
    [string] $importantParameter = $(throw "Please specify the important
    parameter!")
    )

    What is the significance of putting this statement within the $ () ? What
    does the $ represent here?

    Thanks,
    Flea



      My System SpecsSystem Spec

  2. #2


    Kiron Guest

    Re: syntax question on the throw statement

    $( ) is a subexpression. The syntax $(throw <String>) is used in param definitions when you want the parameter to be mandatory. If the Throw statement is not evaluated inside a subexpression, the parser complains when the statement is on the right side of the assignment operator.

    # parser complains
    function f1 ($x = throw 'oops!') {$x}

    # -x is mandatory
    function f1 ($x = $(throw 'oops!')) {$x}
    f1 hi
    fi

    Subexpression are also good for assigning the output of conditional statements, Switch, Foreach, While and For loops to variables. Also, sometimes you need to pipe output from Switch, Foreach, While and For loops, wrapping them in a subexpression does push their output down the pipeline.

    --
    Kiron

      My System SpecsSystem Spec

  3. #3


    Flea Guest

    Re: syntax question on the throw statement

    Thanks Kiron!

    "Kiron" wrote:

    > $( ) is a subexpression. The syntax $(throw <String>) is used in param
    > definitions when you want the parameter to be mandatory. If the Throw
    > statement is not evaluated inside a subexpression, the parser complains
    > when the statement is on the right side of the assignment operator.
    >
    > # parser complains
    > function f1 ($x = throw 'oops!') {$x}
    >
    > # -x is mandatory
    > function f1 ($x = $(throw 'oops!')) {$x}
    > f1 hi
    > fi
    >
    > Subexpression are also good for assigning the output of conditional
    > statements, Switch, Foreach, While and For loops to variables. Also,
    > sometimes you need to pipe output from Switch, Foreach, While and For
    > loops, wrapping them in a subexpression does push their output down the
    > pipeline.
    >
    > --
    > Kiron
    >

      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: syntax question on the throw statement

    Glad to help Flea.

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Josh Einstein Guest

    Re: syntax question on the throw statement

    It's worth mentioning also that in PowerShell 2.0 you can simply mark the
    parameter as mandatory.

    function Blah {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [String] $Name
    )

    "Hello $Name"

    }

    Blah "Jerry"
    Blah "Newman"
    Blah # prompts for name

    Josh

    "Kiron" <Kiron@xxxxxx> wrote in message
    news:59477415-0EC9-412C-AF4C-E24EBF97E45F@xxxxxx

    > $( ) is a subexpression. The syntax $(throw <String>) is used in param
    > definitions when you want the parameter to be mandatory. If the Throw
    > statement is not evaluated inside a subexpression, the parser complains
    > when the statement is on the right side of the assignment operator.
    >
    > # parser complains
    > function f1 ($x = throw 'oops!') {$x}
    >
    > # -x is mandatory
    > function f1 ($x = $(throw 'oops!')) {$x}
    > f1 hi
    > fi
    >
    > Subexpression are also good for assigning the output of conditional
    > statements, Switch, Foreach, While and For loops to variables. Also,
    > sometimes you need to pipe output from Switch, Foreach, While and For
    > loops, wrapping them in a subexpression does push their output down the
    > pipeline.
    >
    > --
    > Kiron

      My System SpecsSystem Spec

  6. #6


    Kiron Guest

    Re: syntax question on the throw statement

    Yes, good point Josh. Also, in the ISE the HelpMessage attribute provides the message for the prompt; in the console you have to type !? to view the message:

    function Blah {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true,
    HelpMessage='Who do yo want to Blah to?')]
    [String] $Name
    )

    "Hello $Name"

    }

    Blah Jerry
    Blah Newman
    Blah # prompts for name


    --
    Kiron

      My System SpecsSystem Spec

  7. #7


    Josh Einstein Guest

    Re: syntax question on the throw statement

    Which I always found a bit redundant... Why can't it just use the
    documentation from the comments? I gotta admit I always took the lazy way
    out with their help file xml when I was making pssnapins in c# because it
    couldn't make use of the c# doc comments. But thanks for reminding me. I
    haven't been using HelpMessage in my modules and should probably start.

    Josh

    "Kiron" <Kiron@xxxxxx> wrote in message
    news:OEN7g2HlJHA.4372@xxxxxx

    > Yes, good point Josh. Also, in the ISE the HelpMessage attribute provides
    > the message for the prompt; in the console you have to type !? to view the
    > message:
    >
    > function Blah {
    > [CmdletBinding()]
    > param(
    > [Parameter(Mandatory=$true,
    > HelpMessage='Who do yo want to Blah to?')]
    > [String] $Name
    > )
    >
    > "Hello $Name"
    >
    > }
    >
    > Blah Jerry
    > Blah Newman
    > Blah # prompts for name
    >
    >
    > --
    > Kiron

      My System SpecsSystem Spec

  8. #8


    DioGenus Guest

    Re: syntax question on the throw statement

    Is there a way to turn of this prompting for missing mandatory parameters and
    instead throw an error (besides using a throw-statement as the parameter
    default value)? Getting a prompt on the console is not very useful in batch
    jobs.

    Dio Genus

    "Josh Einstein" wrote:

    > It's worth mentioning also that in PowerShell 2.0 you can simply mark the
    > parameter as mandatory.
    >
    > function Blah {
    > [CmdletBinding()]
    > param(
    > [Parameter(Mandatory=$true)]
    > [String] $Name
    > )
    >
    > "Hello $Name"
    >
    > }
    >
    > Blah "Jerry"
    > Blah "Newman"
    > Blah # prompts for name
    >
    > Josh
    >
    > "Kiron" <Kiron@newsgroup> wrote in message
    > news:59477415-0EC9-412C-AF4C-E24EBF97E45F@newsgroup

    > > $( ) is a subexpression. The syntax $(throw <String>) is used in param
    > > definitions when you want the parameter to be mandatory. If the Throw
    > > statement is not evaluated inside a subexpression, the parser complains
    > > when the statement is on the right side of the assignment operator.
    > >
    > > # parser complains
    > > function f1 ($x = throw 'oops!') {$x}
    > >
    > > # -x is mandatory
    > > function f1 ($x = $(throw 'oops!')) {$x}
    > > f1 hi
    > > fi
    > >
    > > Subexpression are also good for assigning the output of conditional
    > > statements, Switch, Foreach, While and For loops to variables. Also,
    > > sometimes you need to pipe output from Switch, Foreach, While and For
    > > loops, wrapping them in a subexpression does push their output down the
    > > pipeline.
    > >
    > > --
    > > Kiron
    >
    >

      My System SpecsSystem Spec

  9. #9


    Chris Dent Guest

    Re: syntax question on the throw statement


    I haven't found a way.

    You could use [ValidateNotNullOrEmpty()], however that's implicit with
    Mandatory, and ineffective without (unless the parameter is actually
    supplied).

    Is throw so bad?

    Chris

    DioGenus wrote:

    > Is there a way to turn of this prompting for missing mandatory parameters and
    > instead throw an error (besides using a throw-statement as the parameter
    > default value)? Getting a prompt on the console is not very useful in batch
    > jobs.
    >
    > Dio Genus

      My System SpecsSystem Spec

  10. #10


    DioGenus Guest

    Re: syntax question on the throw statement

    Throw is OK, I just wanted to be updated on the new ways of doing things. The
    new parameter attribute-approcah seems so holistic and complete that I
    suspected that Microsoft had introduced an "attribute-way" that replaced the
    throw-technique, e.g. a [Parameter(Prompt=$false)] statement. Could not find
    anything in the documentation, though...

    BTW: I have already tried [ValidateNotNullOrEmpty()] in combination with
    Mandatory and the prompt still appears. I believe validation takes place
    after data is returned by the prompt.

    Dio Genus

    "Chris Dent" wrote:

    >
    > I haven't found a way.
    >
    > You could use [ValidateNotNullOrEmpty()], however that's implicit with
    > Mandatory, and ineffective without (unless the parameter is actually
    > supplied).
    >
    > Is throw so bad?
    >
    > Chris
    >
    > DioGenus wrote:

    > > Is there a way to turn of this prompting for missing mandatory parameters and
    > > instead throw an error (besides using a throw-statement as the parameter
    > > default value)? Getting a prompt on the console is not very useful in batch
    > > jobs.
    > >
    > > Dio Genus
    > .
    >

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
syntax question on the throw statement problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
First Time User of RoboCopy-Syntax Question SeaRay33 Vista file management 1 12 May 2009
credentials question: seek syntax for domain administratorOpenDSObject arguments gimme_this_gimme_that VB Script 1 20 Mar 2009
syntax question with attached property Lloyd Dupont Avalon 2 21 Jul 2007
Syntax of Pipleline question Larry R PowerShell 2 01 May 2007
Syntax question Jim Holbach PowerShell 2 08 Jun 2006