Reputation: 87
If an app creates an exception, Windows pauses and displays an error dialog box. The user can decide to close the app.
How do I disable this box? (Yes I know about Try, Catch, Finally but I want to disable it globally for the application.) I want to display MyErrorForm.vb instead of the standard error box, or disable it.
Upvotes: 0
Views: 2075
Reputation: 87
I found the answer! Put in Program.cs (Main file) this code:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
(Put it before Application.Run
) and ad error handling:
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//TODO: Handle error...
}
Upvotes: 1
Reputation: 5102
Add code module file to your project, name it UnhandledExceptions.vb followed by adding the following code that replaces the default content.
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
''' <summary>
''' Indicates if we are running under the IDE or not
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property RunningUnderDebugger() As Boolean
Get
Return System.Diagnostics.Debugger.IsAttached
End Get
End Property
Private mUnhandledExceptionsFileName As String
Public Property UnhandledExceptionsFileName() As String
Get
Return mUnhandledExceptionsFileName
End Get
Set(ByVal value As String)
mUnhandledExceptionsFileName = value
End Set
End Property
Private mExceptionDialogIcon As Icon
''' <summary>
''' Specifically used to set the exception dialog's icon
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ExceptionDialogIcon() As Icon
Get
Return mExceptionDialogIcon
End Get
Set(ByVal value As Icon)
mExceptionDialogIcon = value
End Set
End Property
Private mContinueAfterException As Boolean
''' <summary>
''' Determine if this app can stay open after an unhandled
''' exception has been thrown.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>
''' Not practical in most circumstances as we usually
''' do not know how to hangle unhandled exceptions and
''' leaving an app open will not be wise.
''' </remarks>
Public Property ContinueAfterException() As Boolean
Get
Return Not mContinueAfterException
End Get
Set(ByVal value As Boolean)
mContinueAfterException = value
End Set
End Property
''' <summary>
''' Displays a user friendly message in regards to the
''' unhandled exception.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks>
''' It would be wise to also write to a log file etc.
'''
''' WARNING
''' If you use code prone to errors in here then you will not
''' be very happy so test test test before implementing.
''' </remarks>
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
If String.IsNullOrEmpty(mUnhandledExceptionsFileName) Then
mUnhandledExceptionsFileName = "unHandledExceptions.xml"
End If
Dim Doc As New XDocument
If IO.File.Exists(My.Application.UnhandledExceptionsFileName) Then
Doc = XDocument.Load(My.Application.UnhandledExceptionsFileName)
Else
Doc = XDocument.Parse("<?xml version=""1.0"" encoding=""utf-8""?><Exceptions></Exceptions>")
End If
Dim Content = Doc.<Exceptions>(0)
Dim StackTraceText As String = e.Exception.StackTrace.ToString
Content.Add(
<Exception>
<Date_Time><%= Now.ToString("MM/dd/yyyy HH:mm") %></Date_Time>
<Message><%= e.Exception.Message %></Message>
<StackTrace><%= Environment.NewLine %>
<%= StackTraceText %><%= Environment.NewLine %>
</StackTrace>
</Exception>)
Content.Save(My.Application.UnhandledExceptionsFileName)
' here I am using a form setup specifically to show exceptions
'Dim f As New frmExceptionDialog
'f.InBoundException = e.Exception
'f.Message = String.Format("Exception has been recorded to [{0}]", My.Application.UnhandledExceptionsFileName)
'f.Header = "Unhandled exception"
'Try
' f.ShowDialog()
'Finally
' f.Dispose()
'End Try
MessageBox.Show("Encountered an exception" & Environment.NewLine & e.Exception.Message)
e.ExitApplication = Me.ContinueAfterException
If Me.ContinueAfterException Then
Me.ContinueAfterException = False
End If
End Sub
End Class
End Namespace
Example usage in your startup form
Public Class Form1
Private ExceptionLogFile As String = ""
Public Sub New()
InitializeComponent()
My.Application.UnhandledExceptionsFileName = "unHandledExceptions.xml"
ExceptionLogFile = My.Application.UnhandledExceptionsFileName
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If My.Application.RunningUnderDebugger Then
MessageBox.Show("Please execute from Window's Explorer")
Application.ExitThread()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Throw New Exception("Hey")
End Sub
End Class
Output generated
<?xml version="1.0" encoding="utf-8"?>
<Exceptions>
<Exception>
<Date_Time>02/10/2016 15:45</Date_Time>
<Message>Hey</Message>
<StackTrace>
at WindowsApplication3.Form1.Button1_Click(Object sender, EventArgs e) in C:\Dotnet_Development\Projects_Non_Business\WindowsApplication3\Form1.vb:line 16
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
</StackTrace>
</Exception>
</Exceptions>
I used xml but you could easily use a text file or other type of file. When I was doing this type of working with exceptions the code would also send me an email message.
See also which has the custom form to display messages rather than a MessageBox. Dealing with unhandled exceptions in Window's form solutions
IMPORTANT: In the first file there is a property, ContinueAfterException, this under 99 percent of the time be set to false as setting it to true will allow (if possible) the app to stay active but more likely than not unstable.
Upvotes: 0
Reputation: 294237
You need to hook:
See What's the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException? for details.
Upvotes: 0