try catch - "Variable declared but never used" ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • What-A-Tool

    #1

    try catch - "Variable declared but never used" ?

    In the following code, "catch" is underlined in blue, and the error "the
    variable fx is declared but never used" is displayed when I mouse hover over
    it.

    Am I doing something wrong, or do I just ignore it?

    private void btnCalc_Click(o bject sender, System.EventArg s e)

    {

    try

    {

    int intNum = Int32.Parse(txt NumEntry.Text);

    long lngNum = Int64.Parse(txt NumEntry.Text);

    }

    catch (System.FormatE xception fx) //Non integer input

    {

    MessageBox.Show ("Invalid entry!", "Entry Error!",

    MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);

    }

    finally

    {

    //unexpected error

    }

    }



    Thanks - Sean


  • Robbe Morris [C# MVP]

    #2
    Re: try catch - "Variab le declared but never used" ?

    If you aren't going to use it, then don't reference it

    catch (System.FormatE xception)

    --
    Robbe Morris - 2004-2006 Microsoft MVP C#






    "What-A-Tool" <Die!FrigginSpa mmers@DieDieDie .Com> wrote in message
    news:6AtMf.5336 9$Ug4.7762@duke read12...[color=blue]
    > In the following code, "catch" is underlined in blue, and the error "the
    > variable fx is declared but never used" is displayed when I mouse hover
    > over it.
    >
    > Am I doing something wrong, or do I just ignore it?
    >
    > private void btnCalc_Click(o bject sender, System.EventArg s e)
    >
    > {
    >
    > try
    >
    > {
    >
    > int intNum = Int32.Parse(txt NumEntry.Text);
    >
    > long lngNum = Int64.Parse(txt NumEntry.Text);
    >
    > }
    >
    > catch (System.FormatE xception fx) //Non integer input
    >
    > {
    >
    > MessageBox.Show ("Invalid entry!", "Entry Error!",
    >
    > MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
    >
    > }
    >
    > finally
    >
    > {
    >
    > //unexpected error
    >
    > }
    >
    > }
    >
    >
    >
    > Thanks - Sean
    >
    >[/color]


    Comment

    • Boblemar

      #3
      Re: try catch - &quot;Variab le declared but never used&quot; ?

      What-A-Tool a écrit :[color=blue]
      > In the following code, "catch" is underlined in blue, and the error "the
      > variable fx is declared but never used" is displayed when I mouse hover over
      > it.
      >
      > Am I doing something wrong, or do I just ignore it?
      >
      > private void btnCalc_Click(o bject sender, System.EventArg s e)
      >
      > {
      >
      > try
      >
      > {
      >
      > int intNum = Int32.Parse(txt NumEntry.Text);
      >
      > long lngNum = Int64.Parse(txt NumEntry.Text);
      >
      > }
      >
      > catch (System.FormatE xception fx) //Non integer input
      >
      > {
      >
      > MessageBox.Show ("Invalid entry!", "Entry Error!",
      >
      > MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
      >
      > }
      >
      > finally
      >
      > {
      >
      > //unexpected error
      >
      > }
      >
      > }
      >
      >
      >
      > Thanks - Sean
      >
      >[/color]

      You can write the following line into your catch block :
      fx.ToString();
      there won't be any warning anymore and you will have the possibility to
      look at the error at debug time if you want

      Bob

      Comment

      • What-A-Tool

        #4
        Re: try catch - &quot;Variab le declared but never used&quot; ?


        "Boblemar" <arnaud.fonteno y@logicim.com> wrote in message
        news:uMp2Xo3OGH A.1676@TK2MSFTN GP09.phx.gbl...
        [color=blue]
        >
        > You can write the following line into your catch block :
        > fx.ToString();
        > there won't be any warning anymore and you will have the possibility to
        > look at the error at debug time if you want
        >
        > Bob[/color]

        Simple solution :-)

        Just seemed strange, cause I coded it the way I saw in several different
        examples, and nothing was ever mentioned about errors being pointed.

        Thanks - Sean


        Comment

        • Sandeep

          #5
          Re: try catch - &quot;Variab le declared but never used&quot; ?

          This is just a warning. You can change your error level at the compler
          option.

          "What-A-Tool" <Sean@IHateSpam .Com> wrote in message
          news:QdBMf.5338 5$Ug4.43518@duk eread12...[color=blue]
          >
          > "Boblemar" <arnaud.fonteno y@logicim.com> wrote in message
          > news:uMp2Xo3OGH A.1676@TK2MSFTN GP09.phx.gbl...
          >[color=green]
          >>
          >> You can write the following line into your catch block :
          >> fx.ToString();
          >> there won't be any warning anymore and you will have the possibility to
          >> look at the error at debug time if you want
          >>
          >> Bob[/color]
          >
          > Simple solution :-)
          >
          > Just seemed strange, cause I coded it the way I saw in several different
          > examples, and nothing was ever mentioned about errors being pointed.
          >
          > Thanks - Sean
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: try catch - &quot;Variab le declared but never used&quot; ?

            Sandeep <Sandeep@mailin ator.com> wrote:[color=blue]
            > This is just a warning. You can change your error level at the compler
            > option.[/color]

            True, but it's far better to clean up the warning in the way suggested
            by Robbe. Turning off warnings may well cause grief when you come up
            against a problem which would have normally raised a warning.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Bruce Wood

              #7
              Re: try catch - &quot;Variab le declared but never used&quot; ?

              This is a personal bugbear of mine. It's a very useful warning in the
              case of local variables, but it's very annoying in the case of "catch".
              In the case of "catch," if you eliminate the variable name, then as
              Boblemar points out you can't see the exception at debug time: you can
              put a breakpoint in the catch body, but you can't see what exception
              was caught.

              I usually use some stupid trick like Boblemar's in order to fool the
              compiler into shutting up, but I wish that variables declared in
              "catch" clauses were an exception to this message.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: try catch - &quot;Variab le declared but never used&quot; ?

                Bruce Wood <brucewood@cana da.com> wrote:[color=blue]
                > This is a personal bugbear of mine. It's a very useful warning in the
                > case of local variables, but it's very annoying in the case of "catch".
                > In the case of "catch," if you eliminate the variable name, then as
                > Boblemar points out you can't see the exception at debug time: you can
                > put a breakpoint in the catch body, but you can't see what exception
                > was caught.
                >
                > I usually use some stupid trick like Boblemar's in order to fool the
                > compiler into shutting up, but I wish that variables declared in
                > "catch" clauses were an exception to this message.[/color]

                One alternative to actually calling something at runtime would be to
                call a method which was conditionally removed - i.e.

                DevNullUtil.DoN othingWith(e)

                where DoNothingWith had a conditional attribute which would never be
                defined.

                Not something I've ever done myself, but just a thought...

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                Working...