Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...