Results 1 to 10 of 10

Thread: [RESOLVED] try...catch in VBA

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Resolved [RESOLVED] try...catch in VBA

    Is there a try..catch statement in VBA where I can try a statement and then get the VBA or office error if I met any?

    something like:
    Code:
    Dim testVar as Integer
    try {
       testVar = "String";
    } catch(Error e) {
       msgBox("There is an error: " + e.errMsg);
    }

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: try...catch in VBA

    I'm afraid not... for the kind of error handling you can use in VBA, see the article Why do errors crash my program, and how can I stop that from happening? (making an Error Handler) from our Classic VB FAQs

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: try...catch in VBA

    Thanks. Does the statement On Error GoTo ErrorHandler: mean that if any of the lines below this On Error statement has an error, it will jump immediately to the ErrorHandler clause?

    Then say if I only wanted a block of code to have the error handled or need to handle two blocks of codes with possible errors?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: try...catch in VBA

    Quote Originally Posted by xEnOnn View Post
    Thanks. Does the statement On Error GoTo ErrorHandler: mean that if any of the lines below this On Error statement has an error, it will jump immediately to the ErrorHandler clause?
    Correct... and that applies for the entire routine, unless another On Error statement is reached.
    Then say if I only wanted a block of code to have the error handled or need to handle two blocks of codes with possible errors?
    It isn't clear what you mean.

    You can have an error handler deal with errors for more than one block of code, and/or you can have more than one error handler.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: try...catch in VBA

    hmm..I was tryingto say like for eg:

    vb Code:
    1. On Error Goto ErrorHandler1:
    2. 'some codes with possible error
    3. .......
    4. 'Then here, I wanna end the On Error handling
    5.  
    6. 'continue with the codes without errors
    7. ...................
    8.  
    9. 'then start another block of error handling again
    10. On Error Goto ErrorHandler2:
    11. 'second block of codes with possible errors....
    12. ...................
    13. 'End the error handling in this block again
    14.  
    15. ErrorHandler1:
    16. ....
    17.  
    18. Error Handler2:
    19. .....

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: try...catch in VBA

    That is fine, just make sure to have "Exit Sub/Function" as shown in the FAQ article.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: try...catch in VBA

    Is there a End Error Handler statement so that around line 6 to 7 in the example, the errors are no handled by the error handler statement earlier?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: try...catch in VBA

    another On Error statement, either to a different handler you have created, or to 0 (no handler)

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: try...catch in VBA

    Thanks a lot!

  10. #10
    New Member
    Join Date
    Jul 2014
    Posts
    3

    Re: [RESOLVED] try...catch in VBA

    Quote Originally Posted by xEnOnn View Post
    Is there a try..catch statement in VBA where I can try a statement and then get the VBA or office error if I met any?

    something like:
    Code:
    Dim testVar as Integer
    try {
       testVar = "String";
    } catch(Error e) {
       msgBox("There is an error: " + e.errMsg);
    }
    Thought I'd update this since it threw me off.
    Try/Catch now exists

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