Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Anything Wrong with Creating An ExceptionOccurred Event
Message
From
19/08/2009 02:37:22
 
 
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01417957
Message ID:
01418867
Views:
41
Yeah, I wasn't sure what you meant, so I looked at is an excuse to plug my favorite answers. <g>

~~Bonnie


>>>>>I don't understand why you are not using standard exception handling for this? You can handle the exception in the calling method >>but if you forget to (or can't) do so at least it will be trapped at the application level handler.
>>>
>>>Could you explain what you mean by "standard exception handling"?

>>
>>That's when you handle an exception at the very "top" of an application, in your MainForm, in case exception handling as been missed by the developer in other modules, forms or whatever. Something like this will do it:
>
>Hi Bonnie,
>Good example of top-level exception handling but really I was meaning to point Kevin towards the general use of try/catch/finally and throw. Guess I should have said 'structured exception handling' rather then standard......
>Best,
>Viv
>
>>
>>
>>[STAThread]
>>static void Main(string[] args)
>>{
>>	// 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);
>>	
>>	Application.Run(new MainForm());
>>}
>>
>>// Creates a class to handle the exception event.
>>internal class CustomExceptionHandler
>>{
>>	// Handles the exception event.
>>	public void OnThreadException(object sender, ThreadExceptionEventArgs t)
>>	{
>>		DialogResult result = this.ShowThreadExceptionDialog(t.Exception);
>>
>>		// Exits the program after displaying message to the user. 
>>		// In Development mode, the developer will have more options (Abort/Retry/Ignore).
>>		if (result == DialogResult.OK || result == DialogResult.Abort)
>>			Application.Exit();
>>	}
>>
>>	// Creates the error message and displays it.
>>	private DialogResult ShowThreadExceptionDialog(Exception e)
>>	{
>>		DialogResult retval;
>>		string msgUser  = "An error occurred please contact the adminstrator with the following information:\n\n";
>>		string msgDev   = "An unhandled exception occurred (Abort/Retry/Ignore buttons are only displayed to Developers) \n\n";
>>		
>>		string msgTrace = "Error: " + e.Message + "\n\n";
>>		if (e.InnerException != null)
>>			msgTrace   += "       " + e.InnerException.Message + "\n\n";
>>		msgTrace += "Error Method: " + e.TargetSite + "\n\n" +
>>					"Stack Trace: "  + e.StackTrace;
>>
>>		if (System.Diagnostics.Debugger.IsAttached)
>>			retval = MessageBox.Show(msgDev + msgTrace, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);
>>		else
>>			retval = MessageBox.Show(msgUser + msgTrace, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
>>		
>>		return retval;
>>	}
>>}
>>
>>
>>You'll notice that in my example, I only allow the user to Retry or Ignore if they're the developer and debugging. I assume that in such a case, the developer will want to see right off the bat what went wrong and where, but if this happens to a real user, it's typically NOT a good idea to allow them to continue, as it may easily lead to further corruption of data. You can also expand on this class to add error-logging or whatever you wish.
>>
>>~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform