在作Winfrom程序时,有时会遇到一个异常,但是这个异常不知道在什么地方发生的,程序会自动关闭,而后什么也没有了,在网上找到了一种方法,用来捕捉这种异常。oop
出现这种状况的缘由是在程序中某些地方考虑不周全,有异常的状况没有考虑到,可是CLR不会在出错时给出提示(注:有些错误没有捕捉的话会自动弹出错误框,让用户选择关闭程序仍是继续),因此就出了事了。。。spa
这时有一种方法来获得这种异常:线程
1 /// <summary> 2 /// 应用程序的主入口点。 3 /// </summary> 4 [STAThread] 5 static void Main() 6 { 7 Application.EnableVisualStyles(); 8 Application.SetCompatibleTextRenderingDefault(false); 9 Application.Run(new Form1()); 10 } 11 12 static Program() 13 { 14 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 15 } 16 17 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 18 { 19 string strException = string.Format("{0}发生系统异常。\r\n{1}\r\n", DateTime.Now, e.ExceptionObject.ToString()); 20 File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemException.log"), strException); 21 }
这样就把异常记录在日志中了。调试
其实此次的纪录是为了处理一个让人烦恼的异常。程序在开始时没有什么问题,直到运行两天后,发生了异常,而且异常是在一个线程中出现的,有部分的提示信息,信息以下:日志
1 2014-12-1 14:48:02发生系统异常。 2 System.Data.RowNotInTableException: 此行已从表中移除而且没有任何数据。BeginEdit() 将容许在此行中建立新数据 3 在 System.Data.DataRow.GetDefaultRecord() 4 在 System.Data.DataRow.get_Item(Int32 columnIndex) 5 在 异常处理.Form1.DemonstrateRowNotInTableException() 6 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 7 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 8 在 System.Threading.ThreadHelper.ThreadStart() 9 在 System.Data.DataRow.GetDefaultRecord() 10 在 System.Data.DataRow.get_Item(Int32 columnIndex) 11 在 异常处理.Form1.DemonstrateRowNotInTableException() 12 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 13 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 14 在 System.Threading.ThreadHelper.ThreadStart()捕捉开始:
没有说明错误在哪一行,也没有弹出出错对话框,这是有缘由的。code
没有弹出错误对话框的缘由:orm
在通常状况下,若是出现了错误,会出现像下面的对话框。blog
线程中不会出现异常对话框。这个深刻的缘由我也不知道,但愿有明白此缘由的能解释一下。get
没有提示错误的所在行缘由:string
在编译程序后,通常会有一个.pdb文件,这个就是调试使用的信息文件,里面保存了各类和调试有关的信息。在运行程序时,此文件被删除掉(我删除掉的缘由就是在别人使用时不须要有调试这一步)的话,错误就不会精确到哪一行。添加上此文件,错误会出现具体的所在行。另,若是是Released程序,异常所在行提示不太准确,这个缘由我也是不知道,在Debug下提示的行数是很是精确的。若是有人知道这个缘由的话,但愿也说一下,万分感谢!!
1 2014-12-1 15:08:44发生系统异常。 2 System.Data.RowNotInTableException: 此行已从表中移除而且没有任何数据。BeginEdit() 将容许在此行中建立新数据 3 在 System.Data.DataRow.GetDefaultRecord() 4 在 System.Data.DataRow.get_Item(Int32 columnIndex) 5 在 异常处理.Form1.DemonstrateRowNotInTableException() 位置 \C#\Project\异常处理\异常处理\Form1.cs:行号 63 6 在 异常处理.Form1.button2_Click(Object sender, EventArgs e) 位置 \C#\Project\异常处理\异常处理\Form1.cs:行号 38 7 在 System.Windows.Forms.Control.OnClick(EventArgs e) 8 在 System.Windows.Forms.Button.OnClick(EventArgs e) 9 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 10 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 11 在 System.Windows.Forms.Control.WndProc(Message& m) 12 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 13 在 System.Windows.Forms.Button.WndProc(Message& m) 14 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 15 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 16 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 17 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 18 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 19 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 20 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 21 在 System.Windows.Forms.Application.Run(Form mainForm)
上面是在Debug下进行调试获得的信息。有提示行。