+ Reply to Thread
Results 1 to 6 of 6

Catch errors and exit gracefully

  1. #1
    Forum Contributor
    Join Date
    04-12-2006
    Location
    Morgan Hill CA
    Posts
    107

    Catch errors and exit gracefully

    Looking for a way to programatically catch errors and exit gracefully. I have many routines and sheets and modules but sometimes I get an error that I don't expect and it breaks into the debugger. Definately NOT what I want a customer to experience or see.

    I do not want to suppress the error - I want to acknowledge that an error has happened with a message in my own words and exit gracefully with out allowing the user to get dumped into the debugger.

    How can I code this?

    Thanks,

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    See if this gets you going.

    Please Login or Register  to view this content.
    Put the code into a general module and run it. You should get a messagebox with 3.

    Now change 15 / 3 with 15 / 0
    and rerun the code. You will get a messagebox that shows the error number, the standard error description and your own message for that error.


    HTH

    rylo

  3. #3
    Forum Contributor
    Join Date
    04-12-2006
    Location
    Morgan Hill CA
    Posts
    107
    Tried this and it workd for the most part.

    I setup the error handler, which does get called. The handler shows a message and then closes the application. What happens is that Excel shows the error again after I try to exit.

    I've tried the following

    On Error GoTo 0
    Err.Clear


    before I call

    Application.Quit

    and I get the MS VB "End" "Debug" dialog again.....

  4. #4
    Forum Contributor
    Join Date
    04-12-2006
    Location
    Morgan Hill CA
    Posts
    107
    On my last POST I found my error - all is working now.

    BUT

    Here's a follow-on question.


    What I'd like to do is set it once and let it work versus have to spread this error handling around everywhere... Yes, I do have error handleing code elsewhere, but not ALL errors can be accounted for when coding and testing and my original purpose was to avoid my customers from seeing that MS Excel debug dialog box - and insert something more pleasing where I have control over the display and actions.

    Can I set a GLOBAL error_catch for all errors that Excel VBA may throw my way? Set it in my Init() call to handle any thrown errors? In the example code below - I'm trying to setup a SYSTEM WIDE error_handler. When an error occurs that I have NOT captured elsewhere - it calls error_handler().

    Workbook_Open() ' this workbook, sheet object
    Call setup_handler()
    end sub

    public sub setup_handler() ' coded in a module

    on error go to set_handler
    goto handleIt

    set_handler:
    Call error_handler()

    handleIt:

    end sub


    public sub error_handler() ' coded in a module

    If Err > 0 Then
    Select Case Err.Number
    Case 11
    mydesc = "This is my description"
    MsgBox Err.Number & ", " & Err.Description & vbCrLf & mydesc
    End Select
    End If

    end sub
    What I have to do NOW (it seems) is set this error handler anyplace I suspect I may need it.....

    function doesSomething()

    On Error GoTo errhand

    ...some code that may throw an error

    errhand:
    Call error_handler()

    end function

    I guess I'm asking about the "scope" of the error handler... and how I could code something more SYSTEM (program) WIDE.

    Thanks,

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    I don't believe that you can set some sort of generic error handler in this way. I've seen it set in the main calling program for that seems to work.

    Have a look at the code below. The initial calling program sets the error process for the main sub, and any subsequent subs called by the initial program. As per the first example, make xx a 3 and you will see 2 messages (1) is the result and (2) is the "see this" message. If you then change the 3 to a 0 in sub aaa you only see the error handler approach. If you step through the code in the debugger, you will see that it immediately calls the error handler when the error occurs.

    HTH

    rylo

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    04-12-2006
    Location
    Morgan Hill CA
    Posts
    107
    Will give that a spin.

    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1