public class AppException : Exception
{
public AppException()
{
}
public AppException(string message) : base(message)//注意这里
{
}
public AppException(object invalidData, string message)
: base(message + "\n数据:" + invalidData.ToString())//注意这里
{
}
}
单是以上的代码还不行,这里虽然可以做到重新定义了异常,但是当调试时遇到异常是跳不过去的,还要将Main函数作修改。代码如下:
static void
{
CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(eh.OnThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new WinForm());
}
#region 使主程序捕获全局异常后弹出错误的提示
internal class CustomExceptionHandler
{
public void OnThreadException(object sender, System.Threading.ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
result = this.ShowThreadExceptionDialog(t.Exception);
}
catch
{
throw;
}
}
private DialogResult ShowThreadExceptionDialog(Exception e)
{
return MessageBox.Show(e.Message.ToString());
}
}
#endregion