unhandled exception问题

HUSTYZHY 2014-08-12 02:59:05
刚开始学C#,以前学过C++。在C#编写中经常遇到unhandled exception问题,导致程序停止工作。求问,一般怎么解决。
...全文
424 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
HUSTYZHY 2014-08-12
  • 打赏
  • 举报
回复
顺便问一句,可以用VS2010创建XML文档么,一般XML文档用什么创建比较好?
於黾 2014-08-12
  • 打赏
  • 举报
回复
怎么乱用using 既然using了,还需要close? 而且把StreamReader传递给方法里用,这样好么?
HUSTYZHY 2014-08-12
  • 打赏
  • 举报
回复
程序是这样的
using System;
using System.IO;
class Test
{
    public static void Main(string[] args)
    {
        using (StreamWriter w = File.AppendText(@"c:\temp\log.txt"))
        {
            Log("Test1",w);
            Log("Test2",w);
            w.Close();
        }
        using (StreamReader r = File.OpenText(@"c:\temp\log.txt"))
        {
            Logmsg(r);
        }
    }
    private static void Logmsg(StreamReader r)
    {
        String line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
        r.Close();
    }
    private static void Log(string logMessage, TextWriter w)
    {
        w.Write("\r\n开始记录日志信息y:");
        w.WriteLine("{0},{1}",DateTime.Now.ToLongTimeString(),DateTime.Now.ToLongDateString());
        w.WriteLine(" :{0}",logMessage);
        w.WriteLine("---------------------------------");
        w.Flush();
    }
}
HUSTYZHY 2014-08-12
  • 打赏
  • 举报
回复
引用 2 楼 yaotomo 的回复:
在program.cs的main函数中加入代码

static Program()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);           
        }

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string strException = string.Format("{0}发生系统异常。\r\n{1}\r\n\r\n\r\n", DateTime.Now, e.ExceptionObject.ToString());
            File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemException.log"), strException);
        }
要加在main函数里面么?
yaotomo 2014-08-12
  • 打赏
  • 举报
回复
在program.cs的main函数中加入代码

static Program()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);           
        }

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string strException = string.Format("{0}发生系统异常。\r\n{1}\r\n\r\n\r\n", DateTime.Now, e.ExceptionObject.ToString());
            File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemException.log"), strException);
        }
  • 打赏
  • 举报
回复
try catch住

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧