在Main方法中設定異常的最後一次捕捉

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

在做Winfrom程式時,有時會遇到一個異常,可是這個異常不知道在什麼地方發生的,程式會自動關閉,然後什麼也沒有了,在網上找到了一種方法,用來捕捉這種異常。

出現這種情況的原因是在程式中某些地方考慮不周全,有異常的情況沒有考慮到,但是CLR不會在出錯時給出提示(註:有些錯誤沒有捕捉的話會自動彈出錯誤框,讓使用者選擇關閉程式還是繼續),所以就出了事了。。。

這時有一種方法來得到這種異常:

 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()捕捉開始:

沒有說明錯誤在哪一行,也沒有彈出出錯對話方塊,這是有原因的。

沒有彈出錯誤對話方塊的原因:

  在一般情況下,如果出現了錯誤,會出現像下面的對話方塊。

  線程中不會出現異常對話方塊。這個深入的原因我也不知道,希望有明白此原因的能解釋一下。

沒有提示錯誤的所在行原因:

  在編譯器後,一般會有一個.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下進行調試得到的資訊。有提示行。

在Main方法中設定異常的最後一次捕捉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.