Multiple Errors to handle


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Multiple Errors to handle

  1. #1
    bulldog Guest

    Multiple Errors to handle


    See code snippet below. I'm trying to open up possibe Exchange server names
    using possibleservers(x) array. However not all are servers. So I have
    this error handler to catch and not write the valid servers to the validexchangeservers(x)
    array. However this error handler only works for one possibleserver(x) that
    isn't an exchange server. Any other exchange server that isn't an exchange
    server will give a Run-time error as usually. How can I have this code suppress
    the same error multiple times??????


    For x = 0 To countarray
    objCDOEXMDS2.Open possibleservers(x)
    On Error GoTo errhandler:
    validexchangeservers(x) = possibleservers(x)

    errhandler:
    Next x

  2. #2
    Bulldog Guest

    Re: Multiple Errors to handle


    "bulldog" <vb.@127.0.0.1> wrote:
    >
    >See code snippet below. I'm trying to open up possibe Exchange server names
    >using possibleservers(x) array. However not all are servers. So I have
    >this error handler to catch and not write the valid servers to the validexchangeservers(x)
    >array. However this error handler only works for one possibleserver(x)

    that
    >isn't an exchange server. Any other exchange server that isn't an exchange
    >server will give a Run-time error as usually. How can I have this code

    suppress
    >the same error multiple times??????
    >
    >
    >For x = 0 To countarray
    > objCDOEXMDS2.Open possibleservers(x)
    > On Error GoTo errhandler:
    > validexchangeservers(x) = possibleservers(x)
    >
    >errhandler:
    >Next x



    I figured it out. What I had to do was put the ErrHandler in a Function,
    and now it works fine
    Thanks anyways




  3. #3
    Arthur Wood Guest

    Re: Multiple Errors to handle


    Once a error has been encountered, and your error handler has been invoked,
    THE ERROR HAS BEEN HANDLED, and even though you think you are jumping back
    into the loop, it doesn't work that way. Any error encountered which in
    the ERROR_HANDLER, will in fact NOT BE "handled" but will behave exactly
    are you are reporting.

    What you want to do is to use the statement

    RESUME NEXT

    which will "re-invoke" the On Error Goto statement (hence "turiniong error
    handling abck on) and jump to the statement that IMMEDIATELY FOLLOWED the
    line that coused the original error:


    On Error GoTo errhandler:

    For x = 0 To countarray
    objCDOEXMDS2.Open possibleservers(x)
    validexchangeservers(x) = possibleservers(x)
    Next x
    exit sub

    errhandler:
    'do something a bit more intelligent here, exactly what is up to you
    Resume Next

    End Sub


    "bulldog" <vb.@127.0.0.1> wrote:
    >
    >See code snippet below. I'm trying to open up possibe Exchange server names
    >using possibleservers(x) array. However not all are servers. So I have
    >this error handler to catch and not write the valid servers to the validexchangeservers(x)
    >array. However this error handler only works for one possibleserver(x)

    that
    >isn't an exchange server. Any other exchange server that isn't an exchange
    >server will give a Run-time error as usually. How can I have this code

    suppress
    >the same error multiple times??????
    >
    >
    >For x = 0 To countarray
    > objCDOEXMDS2.Open possibleservers(x)
    > On Error GoTo errhandler:
    > validexchangeservers(x) = possibleservers(x)
    >
    >errhandler:
    >Next x



  4. #4
    Dean Earley Guest

    Re: Multiple Errors to handle

    > Once a error has been encountered, and your error handler has been invoked,
    > THE ERROR HAS BEEN HANDLED, and even though you think you are jumping back
    > into the loop, it doesn't work that way. Any error encountered which in
    > the ERROR_HANDLER, will in fact NOT BE "handled" but will behave exactly
    > are you are reporting.
    >
    > What you want to do is to use the statement
    >
    > RESUME NEXT
    >
    > which will "re-invoke" the On Error Goto statement (hence "turiniong error
    > handling abck on) and jump to the statement that IMMEDIATELY FOLLOWED the
    > line that coused the original error:


    but in the current procedure.

    say it calls a function which causes an error, resume next will continue from after the function call.

    Is there a way to resume from the current position apart from:


    on error goto skip

    do

    'DoStuff

    Skip:
    if err>0
    'Do stuff here
    resume Continue
    end if

    Continue:
    Loop

    --
    Dean Earley (dean.earley@icode.co.uk)
    Assistant Developer

    iCode Systems



  5. #5
    Arthur Wood Guest

    Re: Multiple Errors to handle


    Dean,
    If the error occurs in the Function, but there is NO ERROR HANDLING set
    up IN THAT FUNCTION, then the error will propagate UP the call stack until
    an error handler IS encountered. Thus, the Resume Next, in THAT error handler,
    will in fact invoke the NEXT line, after the call to the Function, in the
    procedure which invoked the function. There is no way to return, back to
    the next line IN THE OFFENDING function. The only way to accomplish THAT
    would be to have Error Handlers AT ALL RELEVANT LEVELS in the call sequence
    (which is generally a good idea, for many reasons).

    Also, I don't thisnk you can have an error handler INSIDE a Do...Loop block,
    as you seem to have you sample code structured.

    Arthur Wood


    "Dean Earley" <dean.earley@icode.co.uk> wrote:
    >> Once a error has been encountered, and your error handler has been invoked,
    >> THE ERROR HAS BEEN HANDLED, and even though you think you are jumping

    back
    >> into the loop, it doesn't work that way. Any error encountered which

    in
    >> the ERROR_HANDLER, will in fact NOT BE "handled" but will behave exactly
    >> are you are reporting.
    >>
    >> What you want to do is to use the statement
    >>
    >> RESUME NEXT
    >>
    >> which will "re-invoke" the On Error Goto statement (hence "turiniong error
    >> handling abck on) and jump to the statement that IMMEDIATELY FOLLOWED

    the
    >> line that coused the original error:

    >
    >but in the current procedure.
    >
    >say it calls a function which causes an error, resume next will continue

    from after
    >the function call.
    >
    >Is there a way to resume from the current position apart from:
    >
    >
    >on error goto skip
    >
    >do
    >
    > 'DoStuff
    >
    > Skip:
    > if err>0
    > 'Do stuff here
    > resume Continue
    > end if
    >
    > Continue:
    >Loop
    >
    >--
    >Dean Earley (dean.earley@icode.co.uk)
    >Assistant Developer
    >
    >iCode Systems
    >
    >



  6. #6
    dnagel Guest

    Re: Multiple Errors to handle

    in this code...

    on error goto skip

    do

    'DoStuff

    Skip:
    if err>0
    'Do stuff here
    resume Continue
    end if

    Continue:
    Loop

    The Resume Continue: serves no purpose at all...
    If an error is encountered, the natural execution of
    the code will fall out of the EndIf and then hit the
    Loop statement.

    I have a tendency to do all my error handling inline...

    Public Sub Main()

    On Error Resume Next

    Dim a As Long
    a = "Dennis"

    Select Case Err.Number
    Case 13:
    MsgBox "Wrong datatype man!"
    Case Else
    MsgBox "unexpected error"
    End Select

    a = 1E+24

    Select Case Err.Number
    Case 6:
    MsgBox "OverFlow Error"
    Case Else
    MsgBox "unexpected error"
    End Select

    On Error GoTo 0

    End Sub

    But in the case that you don't want to use the inline
    error trapping method, you can always create a generic
    error handler at the bottom of the sub. And don't forget
    to use an Exit Sub/Function to avoid hitting the error
    handler again...

    Public Sub Main()

    On Error GoTo ErrorSpot:

    Dim a As Long
    Dim d As String
    Dim n As Variant

    d = "Dennis"
    n = 1E+24

    a = d
    Debug.Print a

    a = n
    Debug.Print a

    Exit Sub

    ErrorSpot:

    Select Case Err.Number
    Case 6:
    MsgBox "OverFlow Error"
    n = n - (1E+23)
    Case 13:
    MsgBox "Wrong datatype man!"
    d = "-1"
    Case Else
    MsgBox "unexpected error"
    End Select

    Err.Clear

    Resume

    End Sub




    "Dean Earley" <dean.earley@icode.co.uk> wrote in message news:3cf76f27$1@10.1.10.29...
    | > Once a error has been encountered, and your error handler has been invoked,
    | > THE ERROR HAS BEEN HANDLED, and even though you think you are jumping back
    | > into the loop, it doesn't work that way. Any error encountered which in
    | > the ERROR_HANDLER, will in fact NOT BE "handled" but will behave exactly
    | > are you are reporting.
    | >
    | > What you want to do is to use the statement
    | >
    | > RESUME NEXT
    | >
    | > which will "re-invoke" the On Error Goto statement (hence "turiniong error
    | > handling abck on) and jump to the statement that IMMEDIATELY FOLLOWED the
    | > line that coused the original error:
    |
    | but in the current procedure.
    |
    | say it calls a function which causes an error, resume next will continue from after the function call.
    |
    | Is there a way to resume from the current position apart from:
    |
    |
    | on error goto skip
    |
    | do
    |
    | 'DoStuff
    |
    | Skip:
    | if err>0
    | 'Do stuff here
    | resume Continue
    | end if
    |
    | Continue:
    | Loop
    |
    | --
    | Dean Earley (dean.earley@icode.co.uk)
    | Assistant Developer
    |
    | iCode Systems
    |
    |



  7. #7
    Dean Earley Guest

    Re: Multiple Errors to handle

    "dnagel" <GrandNagel@hotmail.com> wrote in message news:3cf91317$1@10.1.10.29...
    > in this code...
    >
    > on error goto skip
    >
    > do
    >
    > 'DoStuff
    >
    > Skip:
    > if err>0
    > 'Do stuff here
    > resume Continue
    > end if
    >
    > Continue:
    > Loop
    >
    > The Resume Continue: serves no purpose at all...
    > If an error is encountered, the natural execution of
    > the code will fall out of the EndIf and then hit the
    > Loop statement.


    The Resume continue is to reset the error handler. I have a lot of lines in that do loop, and if an error happens in anyone of them,
    I want it to mark and skip that item in the loop.

    Resume takes it back to the line that failed, but I want to continue after the error handler.

    What I have works at the moment, but I was just wondering if there was a way of resumeing from the current position without doing:
    resume Continue
    Continue:

    > I have a tendency to do all my error handling inline...


    Too much code to do this )

    > But in the case that you don't want to use the inline
    > error trapping method, you can always create a generic
    > error handler at the bottom of the sub. And don't forget
    > to use an Exit Sub/Function to avoid hitting the error
    > handler again...


    I suppose mine could be rewrittne as:

    on error goto skip

    do

    'DoStuff

    Continue:
    Loop

    exit sub

    Skip:
    'Do stuff here
    resume Continue

    Is there any real problem with the way im doing it atm?

    --
    Dean Earley (dean.earley@icode.co.uk)
    Assistant Developer

    iCode Systems



  8. #8
    dnagel Guest

    Re: Multiple Errors to handle


    > Is there any real problem with the way im doing it atm?



    I'd say no... as long as you can predict what the error handler
    will do, and can assure yourself that it doesn't have any holes,
    then it really boils down to preference over anything else...

    Your method should work fine... But as far as 'self documenting code'
    goes, an Err.clear would do much better to explain the purpose behind
    the Resume Continue:.

    D.





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center