看来try  cath 结构也不是万能的。

解决方案 »

  1.   

    到windows事件查看器中去看看具体什么原因,然后再确定try catch为什么没有捕获
      

  2.   

    首先你的确认是try里面出现了错误。
      

  3.   

    调试->异常->common Language Runtime Exceptions 选项,看看引发是不是勾上了,勾上的话 即使是程序里捕获了的异常 也会报错,停止工作。
      

  4.   

    我在 messageBox.Show() 这条语句设了断点,然后按F5运行,程序根本就不会停在断点处,而是运行到 Report.PrintPreview(false); 这一句就弹出上述的报错窗口了。
      

  5.   

    Report.PrintPreview(false);  该语句调用了第三方打印控件指令(Grid++Report v.4.5)。
    从网上搜集的信息中有提到:
    c#的try catch能捕捉的信息是在.net框架内支持的异常,而调用API等出现的异常C#根本就捕获不了,能够提示的就是哪块内存为之读不能修改之类的信息。是不是这个原因?
      

  6.   

    c#调用c++写的dll,如果dll内出现空指针引用等错误的话,try catch是捕获不到的。所以dll里代码要写健壮。
      

  7.   

    你在Report.PrintPreview(false);这一句在设一个端点,看看能运行到这里吗?
      

  8.   

    try catch只能捕获当前线程
    如果你是在其它线程出的异常,是无法捕获的
    你可以通过AppDomain.CurrentDomain.UnhandledException这个方法来捕获非主线程的异常
      

  9.   

    网上的信息说,可以通过如下的全局异常捕捉,来捕捉try catch捕捉不到的异常,如:
    namespace myprog
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                string MName = Process.GetCurrentProcess().MainModule.ModuleName;
                string PName = Path.GetFileNameWithoutExtension(MName);
                Process[] myProcess = Process.GetProcessesByName(PName);
                if (myProcess.Length > 1)  
                {
                    MessageBox.Show("本程序不允许重复启动!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    try
                    {
                         //处理未捕获的异常
                         Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                         //处理UI线程异常
                         Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                         //处理非UI线程异常
                         AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
     
                         #region 应用程序的主入口点
     
                         Application.EnableVisualStyles();
                         Application.SetCompatibleTextRenderingDefault(false);
                         Application.Run(new frmWelcome());
     
                         #endregion
     
                    }
                    catch (Exception ex)
                    {
                         string str = "";
                         string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
     
                         if (ex != null)
                         {
                             str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
                             ex.GetType().Name, ex.Message, ex.StackTrace);
                         }
                         else
                         {
                             str = string.Format("应用程序线程错误:{0}", ex);
                         }
     
                         MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                         //LogManager.WriteLog(str); 
                    }
                }
            }        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                 string str = "";
                 string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
                 Exception error = e.Exception as Exception;
                 if (error != null)
                 {
                     str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
                     error.GetType().Name, error.Message, error.StackTrace);
                 }
                 else
                 {
                     str = string.Format("应用程序线程错误:{0}", e);
                 }
     
                 MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 //LogManager.WriteLog(str);
            }
     
            static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                 string str = "";
                 Exception error = e.ExceptionObject as Exception;
                 string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
                 if (error != null)
                 {
                     str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
                 }
                 else
                 {
                     str = string.Format("Application UnhandledError:{0}", e);
                 }
     
                 MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 //LogManager.WriteLog(str);
            }
        }
    }实际运行了一下,还是无法捕捉打印时出现的异常,郁闷ing ......
      

  10.   

    有在try{}里面出现这中状况的,之前做一个串口的读写,读是在分线程死循环读每次读间隔sleep(50),而在主线程会有写数据的操作(在try{}中)(同时读写会造成dll异常,程序崩溃),会导致程序崩溃。 既然你是要引用打印机,应该会有外部的dll吧,应该是调用dll中的方法出现错误的,这个try{}catch{}会捕捉不到。  
      

  11.   

    我现在遇到的问题也差不多,就是用USB转串进行通信,当通信时拔出USB转串,就会造成程序无响应,try-catch没有效果
      

  12.   

    C++使用CancelSynchronousIo来重启IO,解决阻塞,C#还不知道啊!
      

  13.   

    try
     只能捕获 throw 的异常
      

  14.   

    托管之外的错误吧。try成功了,但Windows报错了。