• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

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

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

0
stewdaddy
Asked:
stewdaddy
  • 3
1 Solution
 
mdouganCommented:
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).
0
 
stewdaddyAuthor 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...
0
 
mdouganCommented:
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()
0
 
sagoo33Commented:
Maybe you could put a Try Catch around the first function only and let the exception bubble up and show the message box there?
0
 
mdouganCommented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now