Results 1 to 26 of 26

Thread: On Error Resume Next

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    On Error Resume Next

    What does this do? Does this affect the whole routine? or just the line right after the statement?
    Thanks in advance

    Seahag

  2. #2
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    It affects the procedure it is in from the line of the statement on.
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Thats what i thought.. but it seems to affect the whole Sub.

    I forced it to do an error a few line down from the On error..
    Doesn't do anything.. just ignors it..
    Any Suggestions why?

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    That is what it does. I fit riuns into an error, it SKIPS over it and keeps going...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  5. #5

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    VB Code:
    1. Option Explicit
    2.  
    3. Sub main()
    4. '
    5. On Error Resume Next
    6.     FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _
    7.             "C:\Program Files\toolbox\toolbox.exe"
    8.            
    9.    Test "g:\files\sales\rep3\1036\sdtcost", _
    10.             "C:\Program Files\toolbox\sdtcost"
    11.            
    12.  
    13. Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus
    14.  
    15. End Sub
    16.  
    17.  
    18.  
    19. Function Test(SeverFolder As String, DestFolder As String)
    20. Dim oFileSystem
    21. Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    22. oFileSystem.CopyFolder SeverFolder, DestFolder, True
    23. Set oFileSystem = Nothing
    24. End Function


    The g:\ does not exist.. so i should get the second error al least..
    but i dont..

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    On Error Resume Next is generally used in events (like the Click event, or the Form_Unload event. It doesn't trap or respond to errors, as James stated. It is a method of preventing run time errors that would blow the user back to their desktop.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Orginally posted by SeaHag
    The g:\ does not exist.. so i should get the second error al least..
    but i dont..
    Your On Error Resume Next is at the top of your routine. Therefore, it covers ALL errors in the routine. On Error Resume Next will contine if an error occurs ANYWHERE Below the On Error line. If you recoded your routine like this
    VB Code:
    1. Option Explicit
    2.  
    3. Sub main()
    4. '
    5.    FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _
    6.             "C:\Program Files\toolbox\toolbox.exe"
    7.            
    8.    Test "g:\files\sales\rep3\1036\sdtcost", _
    9.             "C:\Program Files\toolbox\sdtcost"
    10.     On Error Resume Next        
    11.  
    12. Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus
    13.  
    14. End Sub
    your sub would blow up because g:\ didn't exist.

  8. #8
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Originally posted by SeaHag
    VB Code:
    1. Option Explicit
    2.  
    3. Sub main()
    4. '
    5. On Error Resume Next
    6.     FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _
    7.             "C:\Program Files\toolbox\toolbox.exe"
    8.            
    9.    Test "g:\files\sales\rep3\1036\sdtcost", _
    10.             "C:\Program Files\toolbox\sdtcost"
    11.            
    12.  
    13. Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus
    14.  
    15. End Sub\
    16.  
    17.  
    18.  
    19.  
    20. Function Test(SeverFolder As String, DestFolder As String)
    21. Dim oFileSystem
    22. Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    23. oFileSystem.CopyFolder SeverFolder, DestFolder, True
    24. Set oFileSystem = Nothing
    25. End Function


    The g:\ does not exist.. so i should get the second error al least..
    but i dont..

    Instead of On error resume next, try on error goto errhandler

    'Erhandler is where you can handle a specific error message in this case, you know that a Drive doesn't exists, so you could display a message telling the user of the same. Do you get the general idea?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  9. #9

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Cool Thanks all.. Very Helpfull..

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Any errors after it are ignored.

  11. #11
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Originally posted by DiGiTaIErRoR
    Any errors after it are ignored.
    Do you actually read anything anyone else posts?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  12. #12
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Sometimes I do.

  13. #13
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    My post is less technical than the other though.

    Who wouldn't undertsand it?

    You?

  14. #14
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    Any errors after it are ignored.
    Did anyone catch the first reply???
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  15. #15
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    However I wouldnt recommend using Resume Next

    Because if the error that has occured is going to affect a number of things further down your code you can still end up blowing out your application by making it freeze up when it keeps skipping lines as the errors snowball.

    You would be better off trying to trap the error telling the user what went wrong and taking them back to the step they were on before the error occured.

  16. #16
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by rudvs2
    However I wouldnt recommend using Resume Next

    Because if the error that has occured is going to affect a number of things further down your code you can still end up blowing out your application by making it freeze up when it keeps skipping lines as the errors snowball.

    You would be better off trying to trap the error telling the user what went wrong and taking them back to the step they were on before the error occured.
    Generally, Resume Next is okay. Resume is the one you want to avoid.

    However I agree you should trap the error

  17. #17
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Trapping the error in usually perfered, but there are times when ignoring the error (with On Error Resume Next) is the only way to go.

    Case in point: the form unload event where database objects, and recordset objects etc. are closed and set to nothing. There are times when the application is opened, and then closed right away, so setting these objects to nothing, in the Form_Unload event, would result in a runtime error because they were never created or opened. On Error Resume Next handles this situation quite nicely.

  18. #18
    Originally posted by seoptimizer2001
    It affects the procedure it is in from the line of the statement on.
    Not necessarily; like any error handler you can cancel it by using On Error Goto 0.

  19. #19
    DaoK
    Guest
    I use On Error Resume Next only before Compiling it ( final release ) to prevent bug than I didn't see

  20. #20
    On Error Resume Next is only good in ONE scenario: form resizing and control resizing. Other than that, I consider it to be as low and evil as Variant.

  21. #21
    DaoK
    Guest
    You right but I always block in resize like : if form1.width <2000 then form1.width=2001

  22. #22
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    I think that trapping the error is the best way to go. Then after you check the error number use a resume next from the error handler, if it is an error you expected. Otherwise you may be oblivious to a very simple and stupid error that is occuring.
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  23. #23
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by SeaHag
    VB Code:
    1. Option Explicit
    2.  
    3. Sub main()
    4. '
    5. On Error Resume Next
    6.     FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _
    7.             "C:\Program Files\toolbox\toolbox.exe"
    8.            
    9.    Test "g:\files\sales\rep3\1036\sdtcost", _
    10.             "C:\Program Files\toolbox\sdtcost"
    11.            
    12.  
    13. Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus
    14.  
    15. End Sub
    16.  
    17. Function Test(SeverFolder As String, DestFolder As String)
    18. Dim oFileSystem
    19. Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    20. oFileSystem.CopyFolder SeverFolder, DestFolder, True
    21. Set oFileSystem = Nothing
    22. End Function

    The g:\ does not exist.. so i should get the second error al least..
    but i dont..
    When using an On Error statement (no matter if you use Resume Next or Goto LabelName) in a procedure it's valid until the procedure exits or an On Error Goto 0 statement is found.
    So even though you don't have any error checks in your Test procedure above the On Error Resume Next statement in main is still valid since it haven't reached the end of that procedure yet.
    When an error occure in the Test procedure it immidiatly bails out to return to the next line in the Main procedure.
    So in this case it will not reach the line where you set oFileSystem to Nothing.

    Best regards

  24. #24
    WALDO
    Guest

    I agree with filbert1

    I pretty much only use On Error Resume Next when I do form resizing and I don't particularly care whether I handle errors or not. On Error Goto 0 will reset error handling to the original error handling state in the routine.

    Occasionally I use On Error Resume Next with Common DIalog boxes when I have the CancelError proerty set to true and I really don't feel like handling the error, just checking for a FileName length.

    I also use On Error Resume Next in ASP pages because they don't support GoTo labels. If I expect something may cause an error, I can still check the Err Object.

  25. #25

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Thanks for you thoughts all.

    Actually On Error Resume Next is perfect for what I am doing.

    I have a program copy a set of files from the network every morning. I do this to keep everyone updated on what I am doing.

    Some of the people here have mobile computers. So if the network isn't ther .Error.. error .. error ... not so good.
    I hate errors..

    Seems to work ok..

  26. #26
    Hyperactive Member Hampster's Avatar
    Join Date
    Feb 2001
    Location
    On my hamster wheel.
    Posts
    374
    Well, you can always use On Local Error Resume Next, that'll only affect the sub that called it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width