【发布时间】:2015-03-25 10:32:14
【问题描述】:
如果我想写入文件并处理IOExceptions(以防文件被写保护),我会执行以下操作:
try
{
var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)
{
sw.WriteLine(entry);
}
}
catch (IOException ex)
{
return ex.Message;
}
如果我遇到意外的异常,我的程序会崩溃,用户会报告错误,我会修复它。这就是我想要的。
所以我应该添加另一个捕获,例如:
catch (Exception)
{
throw;
}
这会有什么不同吗?最佳做法是什么?
【问题讨论】: