Handling Exceptions in WinForms

J

Jack Wright

Dear All,
In ASP.Net we had...

Sub Page_Error(Source As Object, E As EventArgs)
Dim message As String = "<font face=verdana color=red>" _
& "<h4>" & Request.Url.ToString() & "</h4>"
_
& "<pre><font color='red'>" &
Server.GetLastError().ToString() & "</pre>" _
& "</font>"

Response.Write(message)
End Sub

where from Server.GetLastError we could get the error and display it
neatly to the user...for all the errors that are unhandled in the
Form...is there a similar funtion in WinForms...I tried to search one
but did not get it...

Please help...

Thanks & Regards
Jack
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Jack Wright) scripsit:
In ASP.Net we had...

Sub Page_Error(Source As Object, E As EventArgs)
Dim message As String = "<font face=verdana color=red>" _
& "<h4>" & Request.Url.ToString() & "</h4>"
_
& "<pre><font color='red'>" &
Server.GetLastError().ToString() & "</pre>" _
& "</font>"

Response.Write(message)
End Sub

where from Server.GetLastError we could get the error and display it
neatly to the user...for all the errors that are unhandled in the
Form...is there a similar funtion in WinForms...I tried to search one
but did not get it...

Have a look at 'Application.ThreadException'.
 
T

Tim A.

What's the preferred method of handling unhandled exceptions at the
application level -- this or enclosing Application.Run() in the app's Main()
method with a try...catch block?
 
H

Herfried K. Wagner [MVP]

* "Tim A. said:
What's the preferred method of handling unhandled exceptions at the
application level -- this or enclosing Application.Run() in the app's Main()
method with a try...catch block?
This.
 
R

Rob Windsor [MVP]

You want to dynamically add a handler to the Application.ThreadException
event in the app's Main method. Something like this:

Public Class MyApp
Private Shared _mainForm As MainForm

Public Shared Sub Main()
AddHandler Application.ThreadException, AddressOf
Application_ThreadException

_mainForm = New MainForm
Application.Run(_mainForm)
End Sub

Private Shared Sub Application_ThreadException(ByVal sender As Object, _
ByVal e As System.Threading.ThreadExceptionEventArgs)
DoSomethingWithException(e.Exception)
Application.Exit()
End Sub
End Class

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada



Tim A. said:
What's the preferred method of handling unhandled exceptions at the
application level -- this or enclosing Application.Run() in the app's Main()
method with a try...catch block?
 
U

Uri Dor

Actually, Jason Clark has a complete article on this in MSDN Magazine
June 2004
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top