zoukankan      html  css  js  c++  java
  • 在Main方法中设置异常的最后一次捕捉

    在做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}发生系统异常。
    {1}
    ", 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下进行调试得到的信息。有提示行。

  • 相关阅读:
    vscode sftp 本地目录设置问题
    Zend where or怎么传
    docker如何查看最近创建的容器
    基于apline构建php7+nginx
    修改layui tree组件支持上移下移
    mysql千万级数据表如何删除
    filezilla 链接ftp不显示本地目录 “您没有权限列出该目录内容”
    php使用socket通过tcp通信及php16进制求和校验位计算
    Session must be started before any output has been sent to the browser;问题解决
    mysql数据库中的union和union的区别(示例演示)
  • 原文地址:https://www.cnblogs.com/ddx-deng/p/4134598.html
Copyright © 2011-2022 走看看