error trapping not working

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

    error trapping not working

    On Winxp Pro
    I try to trap an error:

    On error resume next
    If (Err.Number <> 0) Then
    End sub
    End If

    but no matter what the case Err.Number = 0. When I was using visual basic
    6.0 on a win2k pro computer, errors were returned with values other than 0.
    Is there a patch I need to download or do I need to upgrade to VB.Net or
    something?



  • Raoul Watson

    #2
    Re: error trapping not working


    "aaron" <atdawgie@yahoo .com> wrote in message
    news:c4d3su$i30 $1@nntp6.u.wash ington.edu...[color=blue]
    > On Winxp Pro
    > I try to trap an error:
    >
    > On error resume next
    > If (Err.Number <> 0) Then
    > End sub
    > End If
    >
    > but no matter what the case Err.Number = 0. When I was using visual basic
    > 6.0 on a win2k pro computer, errors were returned with values other than[/color]
    0.[color=blue]
    > Is there a patch I need to download or do I need to upgrade to VB.Net or
    > something?
    >[/color]
    "resume next" means exactly that.. continue with the next statement so it
    can't catch errors (basically you are ignoring errors). What you might want
    to do to catch errors is probably:

    Sub SomeSub()
    On Error GoTo ErrorHan

    'code which may generate error

    Exit Sub

    ErrorHan:
    Select Case Err.Number ' Evaluate error number.
    Case xx 'handle it
    Case 'etc
    End Select

    Resume

    End Sub


    Comment

    • J French

      #3
      Re: error trapping not working

      On Wed, 31 Mar 2004 02:55:39 GMT, "Raoul Watson"
      <WatsonR@Intell igenCIA.com> wrote:
      <snip>
      [color=blue][color=green]
      >>[/color]
      >"resume next" means exactly that.. continue with the next statement so it
      >can't catch errors (basically you are ignoring errors). What you might want
      >to do to catch errors is probably:
      >
      >Sub SomeSub()
      > On Error GoTo ErrorHan
      >
      > 'code which may generate error
      >
      > Exit Sub
      >
      >ErrorHan:
      > Select Case Err.Number ' Evaluate error number.
      > Case xx 'handle it
      > Case 'etc
      > End Select
      >
      > Resume
      >[/color]

      Raoul, I totally disagree, both with what you say about
      On Error Resume Next
      and your 'improved' alternative

      I suggest that you check out On Error Resume Next very carefully
      - you will find that
      a) it works
      b) use of it makes simpler code

      To the OP - try this

      Private Sub Command1_Click( )
      Dim I As Integer

      On Error Resume Next
      I = I / 0
      If Err Then MsgBox Err.Description

      End Sub

      Comment

      • aaron

        #4
        Re: error trapping not working

        Thanks, with your hints I found that the problem is the error is not being
        passed to my other function. How do I pass an error object or what is the
        best way to pass an error if I want to pass both the description and the
        number? I have:

        Sub SomeSub()
        On Error Resume Next

        'code that generates error

        If Err Then
        MyError(Err) 'pass error object????
        End If
        End Sub
        ---------------------------
        Sub MyError (MyErrorObject as ErrObject)

        msgbox MyErrorObject.n umber 'it is always 0 because it's not being passed

        End Sub

        Thanks!

        "J French" <erewhon@nowher e.com> wrote in message
        news:406a8496.1 73353487@news.b tclick.com...[color=blue]
        > On Wed, 31 Mar 2004 02:55:39 GMT, "Raoul Watson"
        > <WatsonR@Intell igenCIA.com> wrote:
        > <snip>
        >[color=green][color=darkred]
        > >>[/color]
        > >"resume next" means exactly that.. continue with the next statement so it
        > >can't catch errors (basically you are ignoring errors). What you might[/color][/color]
        want[color=blue][color=green]
        > >to do to catch errors is probably:
        > >
        > >Sub SomeSub()
        > > On Error GoTo ErrorHan
        > >
        > > 'code which may generate error
        > >
        > > Exit Sub
        > >
        > >ErrorHan:
        > > Select Case Err.Number ' Evaluate error number.
        > > Case xx 'handle it
        > > Case 'etc
        > > End Select
        > >
        > > Resume
        > >[/color]
        >
        > Raoul, I totally disagree, both with what you say about
        > On Error Resume Next
        > and your 'improved' alternative
        >
        > I suggest that you check out On Error Resume Next very carefully
        > - you will find that
        > a) it works
        > b) use of it makes simpler code
        >
        > To the OP - try this
        >
        > Private Sub Command1_Click( )
        > Dim I As Integer
        >
        > On Error Resume Next
        > I = I / 0
        > If Err Then MsgBox Err.Description
        >
        > End Sub
        >[/color]


        Comment

        • Larry Serflaten

          #5
          Re: error trapping not working


          "aaron" <atdawgie@yahoo .com> wrote[color=blue]
          > Thanks, with your hints I found that the problem is the error is not being
          > passed to my other function. How do I pass an error object or what is the
          > best way to pass an error if I want to pass both the description and the
          > number? I have:[/color]


          The Err Object is a global object. You should not pass it as a parameter.
          The called sub will see the same error object that was present in the
          caller routine. If you want to clear the error for use in your called sub,
          then you will have to store the Err object values before you do anything
          that would clear them out.

          LFS




          -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
          http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
          -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

          Comment

          • Raoul Watson

            #6
            Re: error trapping not working


            "J French" <erewhon@nowher e.com> wrote in message
            news:406a8496.1 73353487@news.b tclick.com...[color=blue]
            > On Wed, 31 Mar 2004 02:55:39 GMT, "Raoul Watson"
            > <WatsonR@Intell igenCIA.com> wrote:
            > <snip>
            >[color=green][color=darkred]
            > >>[/color]
            > >"resume next" means exactly that.. continue with the next statement so it
            > >can't catch errors (basically you are ignoring errors). What you might[/color][/color]
            want[color=blue][color=green]
            > >to do to catch errors is probably:
            > >
            > >Sub SomeSub()
            > > On Error GoTo ErrorHan
            > >
            > > 'code which may generate error
            > >
            > > Exit Sub
            > >
            > >ErrorHan:
            > > Select Case Err.Number ' Evaluate error number.
            > > Case xx 'handle it
            > > Case 'etc
            > > End Select
            > >
            > > Resume
            > >[/color]
            >
            > Raoul, I totally disagree, both with what you say about
            > On Error Resume Next
            > and your 'improved' alternative
            >[/color]

            I am not sure you read my post carefully.

            1. My method is not "improved" that's a standard method. Even the VB help
            file will show you a similar code for "on error". I have been using that
            method over 25 years.

            2. I never said On error resume next doesn't work, I use it all the time,
            but like I said it resumes with the next statement so in essence "ignoring"
            the error. For example:

            On error resume next
            open "filedoesntexis t" for input as #1
            debug.print "we get here even if file doesn't exist".
            [color=blue]
            > I suggest that you check out On Error Resume Next very carefully
            > - you will find that
            > a) it works[/color]

            See above.

            Your method is fine too. I don't have a problem with you having the "last
            word".


            Comment

            • Bob Butler

              #7
              Re: error trapping not working

              erewhon@nowhere .com (J French) wrote in message news:<406a8496. 173353487@news. btclick.com>...
              <cut>[color=blue]
              > Raoul, I totally disagree, both with what you say about
              > On Error Resume Next
              > and your 'improved' alternative
              >
              > I suggest that you check out On Error Resume Next very carefully
              > - you will find that
              > a) it works
              > b) use of it makes simpler code[/color]

              I actually agree that it can make for simpler code and use it myself
              (probably more often than I should). When recommending it you should
              probably also note that
              c) it adds overhead to the app and slows execution
              d) it can mask bugs if you don't check for errors after any line that
              might generate them

              it's fine for short sections of code but not always best for longer
              routines.

              Comment

              • J French

                #8
                Re: error trapping not working

                On Wed, 31 Mar 2004 22:30:20 GMT, "Raoul Watson"
                <WatsonR@Intell igenCIA.com> wrote:

                <snip>[color=blue]
                >
                >Your method is fine too. I don't have a problem with you having the "last
                >word".[/color]

                I really don't care - as long as you make an informed choice

                Comment

                • J French

                  #9
                  Re: error trapping not working

                  On 31 Mar 2004 16:01:32 -0800, butlerbob@earth link.net (Bob Butler)
                  wrote:

                  <snip>
                  [color=blue]
                  >I actually agree that it can make for simpler code and use it myself
                  >(probably more often than I should). When recommending it you should
                  >probably also note that
                  >c) it adds overhead to the app and slows execution[/color]
                  ... a tad[color=blue]
                  >d) it can mask bugs if you don't check for errors after any line that
                  >might generate them[/color]
                  Very true[color=blue]
                  >
                  >it's fine for short sections of code but not always best for longer
                  >routines.[/color]
                  That is a different subject - but I get your drift

                  Comment

                  • aaron

                    #10
                    Re: error trapping not working

                    It doesn't work.

                    If I do a debug.print Err.Number before I call the other function, it prints
                    out the error number correctly, but once I call the other function and try
                    to do a debug.print within that function I get a 0.


                    "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message
                    news:406b44e2_4 @corp.newsgroup s.com...[color=blue]
                    >
                    > "aaron" <atdawgie@yahoo .com> wrote[color=green]
                    > > Thanks, with your hints I found that the problem is the error is not[/color][/color]
                    being[color=blue][color=green]
                    > > passed to my other function. How do I pass an error object or what is[/color][/color]
                    the[color=blue][color=green]
                    > > best way to pass an error if I want to pass both the description and the
                    > > number? I have:[/color]
                    >
                    >
                    > The Err Object is a global object. You should not pass it as a parameter.
                    > The called sub will see the same error object that was present in the
                    > caller routine. If you want to clear the error for use in your called[/color]
                    sub,[color=blue]
                    > then you will have to store the Err object values before you do anything
                    > that would clear them out.
                    >
                    > LFS
                    >
                    >
                    >
                    >
                    > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                    > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                    > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


                    Comment

                    • Larry Serflaten

                      #11
                      Re: error trapping not working


                      "aaron" <atdawgie@yahoo .com> wrote[color=blue]
                      > It doesn't work.
                      >
                      > If I do a debug.print Err.Number before I call the other function, it prints
                      > out the error number correctly, but once I call the other function and try
                      > to do a debug.print within that function I get a 0.[/color]

                      Show your work. Post a small demo that reproduces that behaviour.

                      LFS






                      -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                      http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                      -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                      Comment

                      Working...