Sandallic Posted December 29, 2004 Posted December 29, 2004 Hello I got this little problem, Im working with an windowsapplication. Whenever an error occures the program must close and no errors are allowed to stop this program. I have been putting "try...catch" within all my procedures, but it seems like it wants to be error messageboxes anyway. Is there anyway I can have one sub that only runs when error occures ? Like my page_load sub, that "handles mybase.Load"? Thx, Sandallic Quote
Administrators PlausiblyDamp Posted December 29, 2004 Administrators Posted December 29, 2004 On way is to handle the Appdomain's UnhandledException event Public Shared Sub Main() 'setup global error handler AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AllErrorsCaughtHere Dim f As New Form1 Application.Run(f) End Sub Private Shared Sub AllErrorsCaughtHere(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) 'deal with errors here End Sub I would still continue to use try catch blocks within procedures as it makes more sense to handle errors specifically whenever possible; the above code will just catch unhandled errors - the problem is you are then potentially keeping yur application alive but no longer know the state of various objects within it due to the previous error. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Sandallic Posted December 29, 2004 Author Posted December 29, 2004 Re: You got this line, Dim f as new Form1 Application.Run(f) That makes a new instance of my Form, Do I have to do that ? Or is it enough with: Public Shared Sub Main() 'setup global error handler AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AllErrorsCaughtHere End Sub Private Shared Sub AllErrorsCaughtHere(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) 'deal with errors here End Sub Quote
kejpa Posted December 29, 2004 Posted December 29, 2004 Or is it enough with: No, you have to start your form from a Sub Main if you want global error handling. HTH /Kejpa Quote
Administrators PlausiblyDamp Posted December 29, 2004 Administrators Posted December 29, 2004 You could probably also do it from the Form_load event of your main form, personally I tend to start my apps from a sub main so I just wrote the code that way. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.