以前用vb6,在api方面,调试和正式运行是有一定区别的, 但是.net有些东西也让我匪夷所思!
我新建了一个窗体 frmMain ,里面加了按钮, 按钮的事件里是 
           throw new  Exception(); 
下面是main 函数, 你可以先掠过:   /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Thread app = null;
            do
            {
                try
                {
                    if (app == null)
                    {
                        app = new Thread(new ParameterizedThreadStart(ShowWindow));
                    }
              
                    if (app.ThreadState != ThreadState.Stopped && app.ThreadState != ThreadState.Unstarted)
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                    else
                    {
  
                        if (_UCanExit == true)
                        {
                            return;
                        }
                        else
                        {
                        
                            switch (app.ThreadState)
                            {
                                case ThreadState.Unstarted:
                                    break;
                                case ThreadState.Stopped:
                                    if (StartTime.AddMinutes(0) > System.DateTime.Now)
                                    {
                                       TCS.Notify.InfoNotify.ShowNotify("程序需要关闭", "程序仅运行了不到一分钟,可能需要联系维护人员!点击这里退出!",new EventHandler(ContentClick ),true );
                                        return;
                                    }
                                    else
                                    {
                                        //("程序需要重启", "程序异常终止,正在尝试重新启动。");
                                    }
                                        break;
                            }
                            app.Start();                        }
                    }
                }
                catch (Exception)
                {
                    app = null;
                }            } while (true);
            // Application.Run(new Form1());
        }
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Application.ExitThread();
        }        private static void ContentClick(object obj, EventArgs e)
        {
            UCanExit = true;
            Application.Exit ();
        }
        private static bool _UCanExit = false;        public static bool UCanExit
        {
            get { return _UCanExit; }
            set { _UCanExit = value; }
        }
下面的代码比较重要,问题就处在这里:
        public static DateTime StartTime;
        public static void ShowWindow(object v)
        {
            try
            {
                frmMain frm = new frmMain();
                StartTime = System.DateTime.Now;
                frm.Show();
                Application.Run(frm);
 
            }
            catch(Exception   ) 
            {
              string s = string.Format("正在运行程序时发生异常:\r\n发生异常:{0}\n\r代码位置:{1}", ex.Message, ex.TargetSite.Name);
            }
     
        }
点击frmMain中的按钮抛出错误,那么在调试时 上面的代码中 try的 catch 里是可以进去并正常运行的, 但是单独运行时会报错,是继续还是退出。  反编译后的代码没有任何变化, 
所以我最后修改了一下:
        public static DateTime StartTime;
        public static void ShowWindow(object v)
        {
            try
            {
                frmMain frm = new frmMain();
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);  
                StartTime = System.DateTime.Now;
                frm.Show();
                Application.Run(frm);
 
            }
            catch(Exception   ) 
            {
              string s = string.Format("正在运行程序时发生异常:\r\n发生异常:{0}\n\r代码位置:{1}", ex.Message, ex.TargetSite.Name);
            }
     
        }
这样,错误是可以捕获的!很奇怪, 不知道为什么!

解决方案 »

  1.   


    //正常运行
    public static DateTime StartTime;
    public static void ShowWindow(object v)
    {
      try
      {
        frmMain frm = new frmMain();
        StartTime = System.DateTime.Now;
        frm.Show();
        Application.Run(frm);
     
      }
      catch(Exception ex
      {
        string s = string.Format("正在运行程序时发生异常:\r\n发生异常:{0}\n\r代码位置:{1}", ex.Message, ex.TargetSite.Name);
      }
         
    }
      

  2.   

    你只写了
    string s = string.Format("正在运行程序时发生异常:\r\n发生异常:{0}\n\r代码位置:{1}", ex.Message, ex.TargetSite.Name);
    ,并没有写入log或show出来,你在运行时不断点怎么知道没捕捉到呢?你换成 throw ex;,看看异常有没有抛出来。
      

  3.   

    调试时 并不抛出异常, 单独运行时会抛出。 同样的代码 。  
     string s = string.Format("正在运行程序时发生异常:\r\n发生异常:{0}\n\r代码位置:{1}", ex.Message, ex.TargetSite.Name);
    后面是有句话, 我删掉了而已。 因为我怀疑是不是后面那句话有问题。 
    结果删掉依然如此。 
      

  4.   

    catch(Exception) 
    =================
    catch(Exception ex)
    string s = string.Format("正在运行程序时发生异常:\r\n发生异常:{0}\n\r代码位置:{1}", ex.Message, ex.TargetSite.Name);
      

  5.   

    catch 不写东西,一切正常;
    写上 throw ex; 抛出异常;符合逻辑。双击exe来执行,怎么点button都没弹出错误框。
    我想应该是其它地方的异常没处理了,比如你的form里面的其它地方。
      

  6.   

    问题在这里:
    frm.Show();
    Application.Run(frm);Application.Run(frm)本来就包含了frm.Show()的,调试的时候估计跟Application没什么关系,但运行的时候就有关系了。