Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Exception Handler - How to
Message
From
07/04/2011 10:20:59
 
 
To
07/04/2011 00:55:17
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
01506276
Message ID:
01506369
Views:
26
Hello Bonnie

Thanks for your reply!

My question is more art a lower level, I've read that the event handler pointers (don't realy know the correct term) are supposed to be in the "main" function, the entry point of the app, the first place called before anything else.

Well, thats my problem - just starting with vb.net and not knowing it, I havn't found one.

So I read that if you don't put one in manually VS adds one to the code at compile time.

Thats why I tried to add it (as shown below) to my code file from the form, but then it complaints.

I can put the creation of the event handlers in the forms load function, but that would be too late - a lot of code has run by then already.



>Hi Gunnar,
>
>Sometimes VB code looks foreign to me. <g> Anyway, what you posted looks a little different than the code I've used. Here's mine, but it's in C# ... sorry. It works, so maybe you can tweak it into VB and see if it's any more useful to you.
>
>
>// in the Main:
>//Creates an instance of the methods that will handle the exception.
>CustomExceptionHandler eh = new CustomExceptionHandler();
>
>//Adds the event handler to to the event.
>Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
>Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
>
>// runs the main form
>Application.Run(new Form1());
>
>
>// then the custom exception handling class
>internal class CustomExceptionHandler
>{
>	// Handles the exception event.
>	public void OnThreadException(object sender, ThreadExceptionEventArgs t)
>	{
>		DialogResult result = DialogResult.Cancel;
>		try
>		{
>			result = this.ShowThreadExceptionDialog(t.Exception);
>		}
>		catch
>		{
>			try
>			{
>				MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
>			}
>			finally
>			{
>				Application.Exit();
>			}
>		}
>
>		// Exits the program when the user clicks Abort.
>		if (result == DialogResult.Abort)
>			Application.Exit();
>	}
>
>	// Creates the error message and displays it.
>	private DialogResult ShowThreadExceptionDialog(Exception e)
>	{
>		string errorMsg = "An error occurred please contact the adminstrator with the following information:\n\n";
>		errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
>		return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
>	}
>}
>
>
>Hope it helps ...
>
>~~Bonnie
>
>
>
>
>
>>I am trying to add an exception handler to my vb.net WinApp - and as usual run into to some problems :-(
>>
>>From my best friend Google I pieced some code together which should accomplish this :
>>Friend Class AppMgr
>>    <STAThread()> _
>>    Shared Sub Main()
>>        ''The 2 event handlers 
>>        ''add an unhandled exceptions handler 
>>        Dim currentDomain As AppDomain = AppDomain.CurrentDomain
>>        'for regular unhandled stuff 
>>        AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler
>>        'for threads behind forms 
>>        AddHandler Application.ThreadException, AddressOf MYThreadHandler
>>
>>        Dim frm1 As Form
>>        frm1 = New DocProcess_Main()
>>        Application.Run(frm1)
>>    End Sub
>>#Region "Exeption Handler"
>>    Shared Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
>>        Dim EX As Exception
>>        EX = e.ExceptionObject
>>        'Console.WriteLine(EX.StackTrace)
>>        DisplayError(EX)
>>    End Sub
>>    Shared Sub MYThreadHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
>>        'Console.WriteLine(e.Exception.StackTrace)
>>        DisplayError(e.Exception)
>>    End Sub
>>    Shared Sub DisplayError(ByVal ex As Exception)
>>        Dim errorMessage As String = ("Unhandled Exception:" + vbLf + vbLf + ex.Message + vbLf + vbLf + ex.GetType.ToString + vbLf + vbLf + "Stack Trace:" + vbLf) + ex.StackTrace
>>        MessageBox.Show(errorMessage, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop])
>>        MessageBox.Show(ex.ToString, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop])
>>    End Sub
>>#End Region
>>End Class
>>
>>Public Class DocProcess_Main
>>    Inherits System.Windows.Forms.Form
>>    ...
>>    ...
>>End Class
>>The error I currently get :
>>
>>Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "WindowsApplication1.DocProcess_Main.resources" was correctly embedded or linked into assembly "DMS DataEntry" at compile time, or that all the satellite assemblies required are loadable and fully signed.
>>
>>My problem: I don't even know if I implemented the code correctly?
>>
>>Any suggestions are great appreciated!
>>
>>Thanks!
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform