Why does this compile?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tni@nc.rr.com

    Why does this compile?

    int x = 0; int y = 0; int z = 0;

    try
    {
    z = x / y;
    }
    catch (Exception) // THIS IS NOT A (DECLARATION)!! !!
    {

    }

    Thomas

  • Michael Bray

    #2
    Re: Why does this compile?

    Why shouldn't it compile? If you want to catch exceptions, you don't have
    to tell it a specific exception variable to store it in...

    -mdb

    tni@nc.rr.com wrote in news:1137706760 .748579.244950
    @g14g2000cwa.go oglegroups.com:
    [color=blue]
    > int x = 0; int y = 0; int z = 0;
    >
    > try
    > {
    > z = x / y;
    > }
    > catch (Exception) // THIS IS NOT A (DECLARATION)!! !!
    > {
    >
    > }
    >
    > Thomas
    >
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Why does this compile?

      <tni@nc.rr.co m> wrote:[color=blue]
      > int x = 0; int y = 0; int z = 0;
      >
      > try
      > {
      > z = x / y;
      > }
      > catch (Exception) // THIS IS NOT A (DECLARATION)!! !!
      > {
      >
      > }[/color]

      C# isn't like Java - you don't have to declare a variable to catch the
      exception "into".

      From the ECMA spec:

      <quote>
      When a catch clause specifies a class-type, the type must be
      System.Exceptio n or a type that derives from System.Exceptio n.

      When a catch clause specifies both a class-type and an identifier, an
      exception variable of the given name and type is declared. The
      exception variable corresponds to a local variable with a scope that
      extends over the catch block. During execution of the catch block, the
      exception variable represents the exception currently being handled.
      For purposes of definite assignment checking, the exception variable is
      considered definitely assigned in its entire scope.

      Unless a catch clause includes an exception variable name, it is
      impossible to access the exception object in the catch block.
      </quote>

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

      • Joerg Jooss

        #4
        Re: Why does this compile?

        Hello tni@nc.rr.com,
        [color=blue]
        > int x = 0; int y = 0; int z = 0;
        >
        > try
        > {
        > z = x / y;
        > }
        > catch (Exception) // THIS IS NOT A (DECLARATION)!! !!
        > {
        > }[/color]

        If you don't use the exception instance within the catch block, you don't
        need to declare an identifier (C# lang spec ยง8.10). Of course you cannot
        access the original exception in this case ;-)

        Cheers,
        --
        Joerg Jooss
        news-reply@joergjoos s.de


        Comment

        • rossum

          #5
          Re: Why does this compile?

          On 19 Jan 2006 13:39:20 -0800, tni@nc.rr.com wrote:
          [color=blue]
          > int x = 0; int y = 0; int z = 0;
          >
          > try
          > {
          > z = x / y;
          > }
          > catch (Exception) // THIS IS NOT A (DECLARATION)!! !!
          > {
          >
          > }
          >
          >Thomas[/color]
          Because there aren't any errors in it.

          rossum

          --

          The ultimate truth is that there is no ultimate truth

          Comment

          • Nick Hounsome

            #6
            Re: Why does this compile?


            <tni@nc.rr.co m> wrote in message
            news:1137706760 .748579.244950@ g14g2000cwa.goo glegroups.com.. .[color=blue]
            > int x = 0; int y = 0; int z = 0;
            >
            > try
            > {
            > z = x / y;
            > }
            > catch (Exception) // THIS IS NOT A (DECLARATION)!! !!
            > {
            >
            > }[/color]

            For 2 related reasons: Code like the following is common

            catch(Exception Type1)
            {
            // the only interesting thing about the exception is its type
            DoStuff();
            }
            catch(Exception Type2)
            {
            // cleanup stuff here but don't use exeception
            throw; // rethrows exception
            }


            and

            catch(Exception ex)
            {
            }

            will give a compiler warning about unused variable ex.

            Of course it seems quite reasonable to me that the compiler should not warn
            in this particular case but that's how it is.


            Comment

            Working...