我正在尝试在catch方法中编写一些代码,并且想要尝试它所生成的异常。但是为了触发catch方法,我需要让程序崩溃,这样才能被捕获并创建异常。
try
{
//do something
}
catch (Exception ex)
{
MessageBox.Show("There was an error, please contact the staff");
using (StreamWriter writer = new StreamWriter(Application.StartupPath + "\\Crashlog\\Crashfile.txt"))
{
writer.WriteLine(ex.ToString());
}
}
现在我想知道,有没有一行简单易记的代码可以确保程序进入catch方法并生成异常?
throw怎么样? - ScoobyDrew18StreamWriter创建/写入Crashfile.txt文件失败怎么办?例如,如果例程从C:\Program Files\MyRoutine(通常的起始路径) 开始,Windows 不允许您在此处创建文件(除非您是管理员)。 - Dmitry Bychenko