Centralised Exception Handling in C# Windows Application
来源:互联网 发布:好听的英文歌曲 知乎 编辑:程序博客网 时间:2024/05/18 00:55
Introduction
You might have been come across a situation when you may need yourapplication to Log all your application errors. This can be done using try catch
blocks.But what about unhandled exceptions. When your application comes acrossan unhandled exception, your application will exit just showing anerror window. This may sometimes result in loss of data. To avoid this,you need to handle all the unhandled exceptions from a centralizedmethod. This article explains a simple way of centralized exceptionhandling.
Using the Code
There are two sources where the exceptions are raised. Either theexception can be raised in the main thread or it can be raised in theadditional threads used in the application. Let us now walk in to thecode how to handle this.
static class Program
{
[STAThread]
static void Main()
{
// Add handler to handle the exception raised by main threads
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// Add handler to handle the exception raised by additional threads
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
// Stop the application and all the threads in suspended state.
Environment.Exit(-1);
}
static void Application_ThreadException
(object sender, System.Threading.ThreadExceptionEventArgs e)
{// All exceptions thrown by the main thread are handled over this method
ShowExceptionDetails(e.Exception);
}
static void CurrentDomain_UnhandledException
(object sender, UnhandledExceptionEventArgs e)
{// All exceptions thrown by additional threads are handled in this method
ShowExceptionDetails(e.ExceptionObject as Exception);
// Suspend the current thread for now to stop the exception from throwing.
Thread.CurrentThread.Suspend();
}
static void ShowExceptionDetails(Exception Ex)
{
// Do logging of exception details
MessageBox.Show(Ex.Message, Ex.TargetSite.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
In the above class, we shall attach an event handler to two events. It is better to attach these events as soon as the main
method starts.
Application.ThreadException
- This event will be raised when an exception is thrown in the main
thread. If we add an event handler, then the exception is handled over the method.
AppDomain.CurrentDomain.UnhandledException
- This eventwill be raised when an exception is thrown in the additional threadsused in the application. The worse scenario here is as soon as thehandlers' execution gets over, the exception is again thrown whereasthe application ends. This need to be handled. Here I have used a bitof code to handle this situation and continue the execution of theapplication without interruption.
The logic I have used to overcome this situation is just suspendingthe thread in the event handler, so that the application continues towork fine. Again a problem arises in suspending this thread. When themain form is closed, the application normally needs to exit, but as thethread is in suspended state, the application will still remainrunning. So to exit the application completely and stop the process, Environment.Exit(-1)
must be called before the ending of the main
method.
Points of Interest
This article enables you to handle any exceptions without forcingyour application to end due to unhandled exceptions in any case.
History
- 20th October, 2009: Initial post
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
About the Author
SHRIDHAR TL
- Centralised Exception Handling in C# Windows Application
- Exception Handling in C#
- Exception Handling in C#
- Manage Exception Handling in Spring Application
- New Vectored Exception Handling in Windows XP
- Exception handling in X++ and C#
- Exception handling in java
- Exception Handling in Java
- Logging & Exception Handling Application Block
- C# Exception Handling
- Exception Handling in Spring MVC
- Exception Handling in Spring MVC
- Exception Handling in Spring MVC
- Exception Handling Application Block (5)详细解
- Handling Events in Windows
- event handling in c#
- Exception Handling Best Practices in .NET
- Exception Handling in WCF using Fault Contract
- 6 款免费的图标编辑器
- java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor的解决方法
- 物料需求计划(MRP)与 企业资源计划(ERP)
- MTK中模拟时钟的绘制
- 浅谈Struts2拦截器的原理与实现
- Centralised Exception Handling in C# Windows Application
- LINUX修改eth*名称
- 使用正则表达式
- AS3自学心得2009年10月30日——滤镜
- 保护个人文档隐私
- 一道亮丽的风景线
- Source Insight查看ARM汇编 && 高亮显示文本 && 标题栏显示全路径 && 从工程批量去除文件 && 添加无扩展名文件 && 快捷键插入时间日期 && 查看PHP源码
- struts2拦截器概述
- USB启动