Err.Number is always = 5

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

    #1

    Err.Number is always = 5

    I have several routines that use the Try-Catch-EndTry method. When an
    error occurs in one of these routines, I display it like this:

    Catch ex As Exception
    MsgBox("Error #" & Err.Number & ": " & Err.Description )
    Exit Function
    End Try

    For some reason, no matter what the error is, the Err.Number is ALWAYS
    "5". I have generated different errors and while the Description
    changes, the error number never does.

    I am sure I am missing something simple & I would appreciate any
    suggestions.

    Thanx!

  • stand__sure

    #2
    Re: Err.Number is always = 5

    Use the properties of the Exception object, ex. Your code is querying
    the Err object which may not be reliable for all exceptions.

    Err.Number = 5 means "Access is denied."

    Comment

    • Kirk

      #3
      Re: Err.Number is always = 5

      Thanks a lot for your quick response.

      I tried to use the properties of the Exception object, but I could not
      find one that shows a unique identifier (like an error number). I
      would like to have this number so I can handle different errors in
      different ways. For example, if the error code equalled "5"
      (indicating an access error), I could show a more "user friendly"
      message in addition to the generic/technical one.

      Specifically, which property would contain this? I tried
      InnerException, Source, GetBaseExceptio n, and TargetSite, but none of
      these was what I was looking for.

      On a related note, can anyone direct me to a listing of the Err.Number
      descriptions?

      Thanx again!

      Comment

      • Rick Mogstad

        #4
        Re: Err.Number is always = 5


        "Kirk" <loki70@hotmail .com> wrote in message
        news:1126631692 .931884.284850@ g43g2000cwa.goo glegroups.com.. .[color=blue]
        > Thanks a lot for your quick response.
        >
        > I tried to use the properties of the Exception object, but I could not
        > find one that shows a unique identifier (like an error number). I
        > would like to have this number so I can handle different errors in
        > different ways. For example, if the error code equalled "5"
        > (indicating an access error), I could show a more "user friendly"
        > message in addition to the generic/technical one.
        >
        > Specifically, which property would contain this? I tried
        > InnerException, Source, GetBaseExceptio n, and TargetSite, but none of
        > these was what I was looking for.
        >
        > On a related note, can anyone direct me to a listing of the Err.Number
        > descriptions?
        >
        > Thanx again!
        >[/color]

        Generally to do that you would catch multiple exception types, rather than a
        single generic exception type



        Comment

        • Kerry Moorman

          #5
          Re: Err.Number is always = 5

          Kirk,

          I don't believe exceptions have a specific number associated with them
          (although the exception's HResult property might be useful).

          Instead, you can catch specific exceptions and supply your own user-friendly
          message. Here is an example that catches various exceptions and supplies an
          application-specific message:

          Catch ex As InvalidCastExce ption
          MsgBox("Input values must be numbers", MsgBoxStyle.Exc lamation,
          "Input Error")

          Catch ex As DivideByZeroExc eption
          MsgBox("The number of donors cannot be zero",
          MsgBoxStyle.Exc lamation, "Input Error")

          Catch ex As Exception
          MsgBox(ex.Messa ge, MsgBoxStyle.Cri tical, "Error")

          End Try

          Kerry Moorman


          "Kirk" wrote:
          [color=blue]
          > Thanks a lot for your quick response.
          >
          > I tried to use the properties of the Exception object, but I could not
          > find one that shows a unique identifier (like an error number). I
          > would like to have this number so I can handle different errors in
          > different ways. For example, if the error code equalled "5"
          > (indicating an access error), I could show a more "user friendly"
          > message in addition to the generic/technical one.
          >
          > Specifically, which property would contain this? I tried
          > InnerException, Source, GetBaseExceptio n, and TargetSite, but none of
          > these was what I was looking for.
          >
          > On a related note, can anyone direct me to a listing of the Err.Number
          > descriptions?
          >
          > Thanx again!
          >
          >[/color]

          Comment

          • Kirk

            #6
            Re: Err.Number is always = 5

            Kerry,

            Thanks for your detailed response! Your example code worked well for
            me. I guess I was still stuck in the "old school" mindset of capturing
            errors by identifying the unique error numbers. Also, I did not really
            understand how the exception Catch terminology worked.

            Thanks again.

            Comment

            • Phill.  W

              #7
              Re: Err.Number is always = 5

              "Kirk" <loki70@hotmail .com> wrote in message
              news:1126631692 .931884.284850@ g43g2000cwa.goo glegroups.com.. .[color=blue]
              > I tried to use the properties of the Exception object, but I could not
              > find one that shows a unique identifier (like an error number).[/color]

              That's because you don't [iften] need one. The *Type* of the
              Exception itself tells you what went wrng, which is why you tend to
              see Catch blocks like this

              Try
              SomethingDodgy( )
              Catch ex as ReallySpecificT ypeOfException
              Recover()
              Catch ex as SomeOtherTypeOf Exception
              Giveup()
              Catch ex as EvenMoreGeneric Exception
              Throw

              ' or, possibly, even
              Catch ex as System.Exceptio n
              If TypeOf ex Is ArgumentExcepti on Then
              OhDearYouDidSom ethingWrong()
              Else
              Throw
              End If
              End Try

              Notice that your Catch'es have to be "in order", catching the
              "smallest" Exception first, i.e. the one "most" derived from
              System.Exceptio n. Check the Class hierarchy for each Exception
              in MSDN; the /fewer/ levels it is away from System.Exceptio n, the
              earlier you should code to catch it. Beware:

              Try
              Catch ex as Exception
              ' /Always/ arrives here
              Catch ex as ArgumentExcepti on
              ' *Never* arrives here
              End Try

              BTW, *some* Exceptions, like the COMException, /do/ have a
              numeric ErrorCode property (which, inthis case, holds the errant
              HResult), but you only get these thrown by methods that "call out"
              to COM. Managed Code doesn't do this so no error "number" is
              necessary.

              HTH,
              Phill W.


              Comment

              Working...