C#try catch

来源:百度知道 编辑:UC知道 时间:2024/05/02 13:09:59
怎样将捕获到的错误保存在本地磁盘比如try{catch(NotFiniteNumberException e)
{
这里应该怎么写到一个TXT文件里面保存到本地磁盘
Console.WriteLine(e.Message);
}

using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
//。。。。。。。。。。。
}
catch(NotFiniteNumberException e)
{
StreamWriter sw = new StreamWriter("D:\\error.txt");
sw.WriteLine(e.ToString());
}
}
}
}

FileStream fs=File.Creat(路径文件名);
byte[] content=new UTF8Encoding(true).GetBytes(e.Message);
fs.Write(content,0,content.Length);
fs.Flush();
fs.Close();
大致是这样来写的 你参考一下 里面还有异常捕捉下

把那个e序列化后保存吧,我不知道NotFiniteNumberException类型是不是可序列化的,如果能序列化,这是最简单的思路,不过你确定要保存这个异常完整内容本身么?好像有点大啊,里面的堆栈跟踪什么的动不动就几页。
不过“try{catch(NotFiniteNumberException e) ”这个写法本身就有问题吧?