We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How do I get my vb.net application to stop running when it errors out?

stewdaddy
stewdaddy asked
on
Medium Priority
792 Views
Last Modified: 2012-05-06
I have a Try/Catch set up.  When it hits Catch, I want it to stop going any further.  I don't want to end the program, I just want to alert the user with a message box and then stop processing.  I know this has got to be simple...
Catch
            MessageBox.Show("Array_lstCells Function error.  Make sure that the datasets you selected are named properly.  If the problem persists, please contact the administrator.", "Array_lstCells Function Error...", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Open in new window

Comment
Watch Question

Commented:
Well, you have to define for us what you mean by "stop going any further".

If you want it to stop executing any further instructions in the current procedure, then you would code an Exit Sub   (or Exit Function if it is a function) immediately after the MessageBox.Show statement.  But, be sure you do any house-cleaning before you do (ie, close and dispose of any Datasets, spreadsheets etc).

Author

Commented:
I want to stop executing any further instructions in the program completely.  The function is being called by another procedure, so as of now when it shows the messagebox it then goes back to executing the procedure it was called from.  That procedure is being called by another procedure, and so on...

Commented:
Well, the only way to stop executing any further instructions in the program completely is to end it with either the END instruction or the Application.Exit() instruction.  But, you said that you didn't want to end the program, so, it is a little confusing what it is you want.

END and EXIT are a little tricky if you are also displaying a message box to the user, because you are killing the thread that spawned the message box while it is still visible on the screen...

Try doing your:

Catch
            MessageBox.Show("Array_lstCells Function error.  Make sure that the datasets you selected are named properly.  If the problem persists, please contact the administrator.", "Array_lstCells Function Error...", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Application.Exit()
        End Try

If this basically does what you want, but generates an error because of the message box, then you might have to code your own Dialog box to display the message using ShowDialog.  That will freeze the execution until the user clicks OK, and then you could unload your Dialog box and then safely call the Application.Exit()
Commented:
Maybe you could put a Try Catch around the first function only and let the exception bubble up and show the message box there?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
It is also possible, in VB.NET, to define your own Global default error handler, which will get executed anytime the program encounters an un-handled error.  I'd have to lookup the exact syntax, but it isn't hard to do.  

But, the question is how, exactly, do you want your error handling to work?

It is possible in the Catch block to Raise another error, which will then bubble back to the previous error handling block, where you can raise another error which will bubble back etc.  So, you can control how far back your error handlers backstep.

But, if you just want to go back to the main code where you entered into the processing, then sagoo33's idea is easiest.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.