CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Exception Handling without closing program

    Hello,

    I've written a small program for a small business, and I'm looking to handle some exceptions in a special way. Specifically, when an attempt to connect to the database fails, I would like to pop up a Windows.Form displaying the exception and giving the user some debug tips.

    Of course, if I catch the exception and throw it forward it closes the entire application, and if I simply catch the exception and don't throw it further it will run into other exceptions, causing the same issue.

    Is there some way I can either cause the current thread to close (opening the information form in another thread), or handle the exception without causing the program to close?

  2. #2
    Join Date
    Nov 2010
    Posts
    34

    Re: Exception Handling without closing program

    create the form on the fly with custom text within the (catch exception e) and showdialog() to pause the thread?
    or just use a goto hehe

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Exception Handling without closing program

    The method that creates the connection to the DB should not be doing anything UI related. That method will simply throw/bubble-up an exception and the UI code can handle it.

  4. #4
    Join Date
    Dec 2010
    Posts
    12

    Re: Exception Handling without closing program

    "and if I simply catch the exception and don't throw it further it will run into other exceptions, causing the same issue."

    Why so if you already caught the exception, why would you proceed, there is definitely some flaw in logic.

  5. #5
    Join Date
    Dec 2010
    Posts
    3

    Re: Exception Handling without closing program

    Quote Originally Posted by harrisunderwork View Post
    "and if I simply catch the exception and don't throw it further it will run into other exceptions, causing the same issue."

    Why so if you already caught the exception, why would you proceed, there is definitely some flaw in logic.
    Perhaps communication more than logic. I don't want it to proceed. I just want it to throw up a UI telling the user some troubleshooting tips, since I won't be around. (Make sure database computer is on, connected, and that database has started, etc). How do I stop it from proceeding without killing the entire application?

    By default, if I just catch the exception and don't throw another exception, the program continues to run, and since there's no connection to the DB it fails somewhere else further down the line.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Exception Handling without closing program

    Did you read my answer?

  7. #7
    Join Date
    Dec 2010
    Posts
    3

    Re: Exception Handling without closing program

    Quote Originally Posted by BigEd781 View Post
    Did you read my answer?
    Yes. That sounds like a good design practice. I'll keep my connection code based on connection only.

    The only solution I've seen(and haven't tried yet) is kungpaoshizi's. That might work, but it seems like there should be something basic about handling exceptions that I'm missing, and I'd like to find out what that is if possible.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Exception Handling without closing program

    I don't quite understand

    Code:
    // in the UI
    try
    {
        myDbObject.Connect( ... );
    }
    catch (SomeDBConnectionException)
    {
        MessageBox.Show( "Couldn't Connect" );
    }

  9. #9
    Join Date
    Dec 2010
    Posts
    3

    Re: Exception Handling without closing program

    Quote Originally Posted by BigEd781 View Post
    I don't quite understand

    Code:
    // in the UI
    try
    {
        myDbObject.Connect( ... );
    }
    catch (SomeDBConnectionException)
    {
        MessageBox.Show( "Couldn't Connect" );
    }

    The problem is after a connect statement there's nothing telling the program to stop, so it will continue on and try to do something with the failed connection, generating a new exception which closes the program.

    Putting the try block around everything might work, but then I'd be spouting out technical stack information instead of the troubleshooting steps I want the user to have.

    Maybe my best option is to make some sort of a parser to run the error message through to generate the feedback I want.

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Exception Handling without closing program

    You could set a flag, and not proceed if the flag is tripped, or generate a trigger to a different error handler
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Exception Handling without closing program

    Well, there is. You can exit the program from the catch block, you can disable controls until it succeeds, etc.

  12. #12
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Exception Handling without closing program

    Quote Originally Posted by Sythion View Post
    [...] but then I'd be spouting out technical stack information instead of the troubleshooting steps I want the user to have.
    Well, you're right about that not being very desirable...
    Exceptions, in their raw, natural state, are in fact meant for the the developer, not the end user, and thus - from a developer's perspective - contain quite a bit of important information that could help solve the problem.

    If your code fails to connect, and then, as you said, it just continues to do it's business as if nothing is wrong, tossing calls to an invalid object, then maybe it could be redesigned to behave more appropriately?
    It may not require a lot of work - see what you can do.

    Then, when you fixed all that was in your power to fix, and a possibility for an exception remains, you can elegantly catch and handle it.
    As BigEd781 said, you can do pretty much anything. Handle any internal stuff that needs to be restored to a valid state, show an appropriate message to the user, or more.
    You can ask the user if the app should try again or quit, or you can check the app's settings to see how it should behave, or you can present the user with a freakin Snake game to kill time untill the connection is established .

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

Featured