Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB.Net 2010: Stop code execution, but stay in form

Posted on 2014-08-25
6
Medium Priority
?
4,623 Views
Last Modified: 2014-08-26
The question is pretty simple.  If I encounter an error, and I'm running VB.Net from the executable file I created, I would like to write code that allows me to abandon the execution and send the control back to the functional form.  If the error occurs from a deeply embedded place within code, my strategy has been to create an elaborate architecture of sending the error code back up the successive ranks, finally getting to the original calling routine, from which exiting sends control back to the form.  I'm looking for a shortcut to perform the same thing.

The "Exit Function" and "Exit Sub" routines take me out of the problematic subroutine or function, but the code continues executing from whatever process in which the errant routine was embedded, such as a "for" or "while" loop.  On the other hand, "End" effectively stops all processes, but also has the undesirable side effect of shutting down the form.  If I encounter an error with my input, I want to be able to stop code execution while leaving the form open, allowing me to modify parameters and try again.  

I'm looking for a statement that is more encompassing than "Exit" but stops short of the finality of "End" (a wished-for routine one might call "Exit All").  Does such a thing exist?

Thanks!  ~Peter Ferber
0
Comment
Question by:PeterFrb
6 Comments
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 40283846
If by error, you mean exception, the exception handling of .Net will cause the exception to bubble back up the stack until it is either caught or causes a "run-time" error (effectively stopping your application).

If the error is a validation issue, you should consider checking for that stuff up front prior to sending data into the depths of your application.
0
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 40283857
If you are using subs to perform your processing, you could change them to functions and return a value indicating whether the function exited successfully or not. If you are using functions already, you could add an additional variable to the call and use it to return a success of failure.
0
 

Author Comment

by:PeterFrb
ID: 40284515
Yes, these are things I do, it's just a little complex if the routine is embedded several layers deep, then it's a matter of getting that return value all the way up the chain.  What I'm hearing you say, thus far, is there's no shortcut around this layering.  That may be the true, but it isn't the thing for which I'd hoped.  

The issue I'm addressing is validation related, and it's sometimes easier to have the program do the error checking than have me be responsible for it beforehand.  Also, those who inherit this code are not as savvy as I am, so I need to make sure the program provides sensible and relevant feedback and doesn't just shut them down when they make a mistake.  

There may, in fact, be no shortcut around what I'm already doing.  I'll let the question remain open for a bit, and see if I get more feedback.  Thanks!  ~Peter
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40284980
You can use a try...catch block.  An error (exception) will move up as many layers as there are until caught by the catch.
That is what you are asking for.  Now for the (inevitable) but.
BUT that is very bad design.  It is really meant for unforseen errors and does not give the routines inbetween the possibility to finish nicely and tidy up.  All you know is something went wrong, not how much was done correctly and what is not yet completed.

What has been suggested (and what I think you are currently doing) is the 'best' way.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 40285611
An exception is either for something that you know could happen but on which you have no control (such as a broken database connection), or for something that you did not expect at all.

In the first case, a properly built Try...Catch block will let you recuperate and do whatever you want.

In the second case, the application is in an unknown situation, and it could be dangerous to go on. This could lead to other problems down the line, corrupted files or databases, who knows, this is something unexpected. The best thing to do there is simply to report or log the error somewhere and stop the application. Find out what is causing the error, handle it and distribute a new version of the application where it is corrected.

This being said, you can bring unexpected exception to a central processing point with very little code.

For this to work, you should write your application so that it start in a Sub Main in a module, and point to that sub as the Startup Object for your application. Basically, that module would look something like the following:

Module Module1

	Public Sub Main()

		AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
		AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler

		Application.Run(New Form1)

	End Sub

	Private Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
		YourReportingMethod(CType(e.ExceptionObject, Exception))
	End Sub

	Private Sub ThreadExceptionHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
		YourReportingMethod(e.Exception)
	End Sub

	Private Sub YourReportingMethod(e As Exception)
		'Report to a file, a database, the event log, in an e-mail... Here we simplify
		MessageBox.Show(e.Message)
	End Sub

End Module

Open in new window


Any error that is not in a Try...Catch block will end up in the last routine.

If you finish your Try...Catch blocks with a Catch/Throw, as in the following, unexpected errors will also bubble up to that routine:
​Try
   'Your code
Catch ex As SqlException
   'SQL Exception handling
Catch ex As IO.EndOfStreamException
   'Unexpected end of the file
Catch ex As IOException
   'File Exception handling
Catch
   Throw
End Try

Open in new window

I am planning a more complete article on the subject in the coming days. Keep a watch for Centralized Exception handling in .NET Windows Applications
0
 

Author Closing Comment

by:PeterFrb
ID: 40286842
Thank you!  That's so elegant, and when this site scores, it really sings.  I will try your solution and report back!
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question