Get Error Number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RP

    Get Error Number

    If an exception occurs, how to get the error number. I mean, is there
    anything like ex.number?

  • John Duval

    #2
    Re: Get Error Number

    On Oct 10, 2:04 pm, RP <rpk.gene...@gm ail.comwrote:
    If an exception occurs, how to get the error number. I mean, is there
    anything like ex.number?
    Hi RP,
    There really isn't anything equivalent to an error number in .NET --
    typically the type of the exception object thrown is what is used to
    do runtime error handling. And the message in the exception is
    typically what is shown to the user, not a number. There are some
    types of exceptions that have error numbers (see
    COMException.Er rorCode) but I think in the new .NET world, the error
    number has gone by the wayside.

    Is there a particular case where you need to get at the error number
    (a la GetLastError from the Win32 days)?

    John

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Get Error Number

      RP <rpk.general@gm ail.comwrote:
      If an exception occurs, how to get the error number. I mean, is there
      anything like ex.number?
      For many exceptions there *isn't* an error number - after all, you can
      create whatever exception you want and throw it.

      You can use Exception.HResu lt in some cases, but don't rely on it
      always being present.

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

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Get Error Number

        Hi,

        "RP" <rpk.general@gm ail.comwrote in message
        news:1192039451 .246765.219050@ 19g2000hsx.goog legroups.com...
        If an exception occurs, how to get the error number. I mean, is there
        anything like ex.number?
        >
        Not really, you can get the message of the exception and even the stacktrace
        that indicate in which method the exception was thrown.



        Comment

        • Chris Mullins [MVP - C#]

          #5
          Re: Get Error Number

          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
          RP <rpk.general@gm ail.comwrote:
          >If an exception occurs, how to get the error number. I mean, is there
          >anything like ex.number?
          >
          For many exceptions there *isn't* an error number - after all, you can
          create whatever exception you want and throw it.
          heh. Perfectly valid, if not quite correct, code...

          Object errorNumber = (object) 101;
          throw errorNumber;

          One day I'm going to figure out why any arbitrary thing can be thrown,
          rather than constraining it to only Exceptions...

          --
          Chris Mullins


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Get Error Number

            Actually, it's not valid C# code. While the CLR does allow for objects
            to be thrown, in C#, the compiler requires that the target of the throw
            statement derive from System.Exceptio n.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m


            "Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
            news:%23KWu2z2C IHA.5976@TK2MSF TNGP02.phx.gbl. ..
            "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
            news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
            >RP <rpk.general@gm ail.comwrote:
            >>If an exception occurs, how to get the error number. I mean, is there
            >>anything like ex.number?
            >>
            >For many exceptions there *isn't* an error number - after all, you can
            >create whatever exception you want and throw it.
            >
            heh. Perfectly valid, if not quite correct, code...
            >
            Object errorNumber = (object) 101;
            throw errorNumber;
            >
            One day I'm going to figure out why any arbitrary thing can be thrown,
            rather than constraining it to only Exceptions...
            >
            --
            Chris Mullins
            >
            >

            Comment

            • RP

              #7
              Re: Get Error Number

              In VB.NET we have err.Number.

              Comment

              • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

                #8
                Re: Get Error Number

                According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
                System.Exceptio n or not, will propagate into C# assemblies as derived from
                System.Exceptio n. .... the CIL code corresponding to an empty catch block is,
                in fact, a catch(object) block..."
                -- Peter
                Recursion: see Recursion
                site: http://www.eggheadcafe.com
                unBlog: http://petesbloggerama.blogspot.com
                BlogMetaFinder: http://www.blogmetafinder.com



                "Nicholas Paldino [.NET/C# MVP]" wrote:
                Actually, it's not valid C# code. While the CLR does allow for objects
                to be thrown, in C#, the compiler requires that the target of the throw
                statement derive from System.Exceptio n.
                >
                >
                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m
                >
                >
                "Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
                news:%23KWu2z2C IHA.5976@TK2MSF TNGP02.phx.gbl. ..
                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
                RP <rpk.general@gm ail.comwrote:
                >If an exception occurs, how to get the error number. I mean, is there
                >anything like ex.number?
                >
                For many exceptions there *isn't* an error number - after all, you can
                create whatever exception you want and throw it.
                heh. Perfectly valid, if not quite correct, code...

                Object errorNumber = (object) 101;
                throw errorNumber;

                One day I'm going to figure out why any arbitrary thing can be thrown,
                rather than constraining it to only Exceptions...

                --
                Chris Mullins
                >
                >
                >

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Get Error Number

                  Which is correct, but it doesn't validate Chris's post, which indicates
                  that you can actually throw something in C# which does not derive from
                  Exception.

                  You could do this in C++/CLI or in IL, but not in C#. And yes, you can
                  catch it with the catch block which doesn't have an exception argument.

                  I actually did some of the review work on that book, and know Mark, so I
                  know exactly which section you are referring to. =)

                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "Peter Bromberg [C# MVP]" <pbromberg@yaho o.yohohhoandabo ttleofrum.comwr ote
                  in message news:51EC8543-CC2B-4A00-BFE8-01DF6B20428D@mi crosoft.com...
                  According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
                  System.Exceptio n or not, will propagate into C# assemblies as derived from
                  System.Exceptio n. .... the CIL code corresponding to an empty catch block
                  is,
                  in fact, a catch(object) block..."
                  -- Peter
                  Recursion: see Recursion
                  site: http://www.eggheadcafe.com
                  unBlog: http://petesbloggerama.blogspot.com
                  BlogMetaFinder: http://www.blogmetafinder.com
                  >
                  >
                  >
                  "Nicholas Paldino [.NET/C# MVP]" wrote:
                  >
                  > Actually, it's not valid C# code. While the CLR does allow for
                  >objects
                  >to be thrown, in C#, the compiler requires that the target of the throw
                  >statement derive from System.Exceptio n.
                  >>
                  >>
                  >--
                  > - Nicholas Paldino [.NET/C# MVP]
                  > - mvp@spam.guard. caspershouse.co m
                  >>
                  >>
                  >"Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
                  >news:%23KWu2z2 CIHA.5976@TK2MS FTNGP02.phx.gbl ...
                  "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
                  >RP <rpk.general@gm ail.comwrote:
                  >>If an exception occurs, how to get the error number. I mean, is there
                  >>anything like ex.number?
                  >>
                  >For many exceptions there *isn't* an error number - after all, you can
                  >create whatever exception you want and throw it.
                  >
                  heh. Perfectly valid, if not quite correct, code...
                  >
                  Object errorNumber = (object) 101;
                  throw errorNumber;
                  >
                  One day I'm going to figure out why any arbitrary thing can be thrown,
                  rather than constraining it to only Exceptions...
                  >
                  --
                  Chris Mullins
                  >
                  >
                  >>
                  >>
                  >>

                  Comment

                  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

                    #10
                    Re: Get Error Number

                    Yep, you are correct -- in C# 2.0 Chris's example code is not valid - you
                    would get "type object does not extend System.Exceptio n" in Intellisense, and
                    a similar compiler error.

                    -- Peter
                    Recursion: see Recursion
                    site: http://www.eggheadcafe.com
                    unBlog: http://petesbloggerama.blogspot.com
                    BlogMetaFinder: http://www.blogmetafinder.com



                    "Nicholas Paldino [.NET/C# MVP]" wrote:
                    Which is correct, but it doesn't validate Chris's post, which indicates
                    that you can actually throw something in C# which does not derive from
                    Exception.
                    >
                    You could do this in C++/CLI or in IL, but not in C#. And yes, you can
                    catch it with the catch block which doesn't have an exception argument.
                    >
                    I actually did some of the review work on that book, and know Mark, so I
                    know exactly which section you are referring to. =)
                    >
                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard. caspershouse.co m
                    >
                    "Peter Bromberg [C# MVP]" <pbromberg@yaho o.yohohhoandabo ttleofrum.comwr ote
                    in message news:51EC8543-CC2B-4A00-BFE8-01DF6B20428D@mi crosoft.com...
                    According to Michaelis, "In C# 2.0, all exceptions, whether deriving from
                    System.Exceptio n or not, will propagate into C# assemblies as derived from
                    System.Exceptio n. .... the CIL code corresponding to an empty catch block
                    is,
                    in fact, a catch(object) block..."
                    -- Peter
                    Recursion: see Recursion
                    site: http://www.eggheadcafe.com
                    unBlog: http://petesbloggerama.blogspot.com
                    BlogMetaFinder: http://www.blogmetafinder.com



                    "Nicholas Paldino [.NET/C# MVP]" wrote:
                    Actually, it's not valid C# code. While the CLR does allow for
                    objects
                    to be thrown, in C#, the compiler requires that the target of the throw
                    statement derive from System.Exceptio n.
                    >
                    >
                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard. caspershouse.co m
                    >
                    >
                    "Chris Mullins [MVP - C#]" <cmullins@yahoo .comwrote in message
                    news:%23KWu2z2C IHA.5976@TK2MSF TNGP02.phx.gbl. ..
                    "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                    news:MPG.217728 24c9f03b1e51b@m snews.microsoft .com...
                    RP <rpk.general@gm ail.comwrote:
                    >If an exception occurs, how to get the error number. I mean, is there
                    >anything like ex.number?
                    >
                    For many exceptions there *isn't* an error number - after all, you can
                    create whatever exception you want and throw it.

                    heh. Perfectly valid, if not quite correct, code...

                    Object errorNumber = (object) 101;
                    throw errorNumber;

                    One day I'm going to figure out why any arbitrary thing can be thrown,
                    rather than constraining it to only Exceptions...

                    --
                    Chris Mullins


                    >
                    >
                    >
                    >
                    >
                    >

                    Comment

                    • Willy Denoyette [MVP]

                      #11
                      Re: Get Error Number

                      "RP" <rpk.general@gm ail.comwrote in message
                      news:1192039451 .246765.219050@ 19g2000hsx.goog legroups.com...
                      If an exception occurs, how to get the error number. I mean, is there
                      anything like ex.number?
                      >

                      You can always retrieve the HRsesult property, but this requires to reflect
                      on a protected property [1], so I would advise against it in production
                      code.

                      [1]
                      catch(Exception ex)
                      {
                      Type t = typeof(Exceptio n);
                      int v = (int) t.InvokeMember( "HResult",
                      BindingFlags.De claredOnly |
                      BindingFlags.No nPublic |
                      BindingFlags.In stance |
                      BindingFlags.Ge tProperty, null, ex, null);

                      Console.WriteLi ne("HResult: {0:x}", v);
                      }

                      Following will output HResult: 80131508

                      try {
                      int[] ia = new int[2];
                      ia[2] = 1;
                      }
                      include [1] here.

                      while throw new Exception("What ever");

                      will output HResult: 80131500, which is the HResult code for CLR Exception.
                      Note that you can define your own HResult codes (respecting the Win32/COM
                      rules applicable to HRESULT codes) to be used in your Exception derived
                      classes.


                      Comment

                      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                        #12
                        Re: Get Error Number

                        RP wrote:
                        If an exception occurs, how to get the error number. I mean, is there
                        anything like ex.number?
                        The information about the type of exception is carried
                        in the type of the exception.

                        Arne

                        Comment

                        Working...