为什么不建议(在最好的实际意义上)从入口点管理系统的所有异常。
class Program
{
static void Main(string[] args)
{
try
{
[...]//all the program stuff
}catch(Exception ex)
{
[...]
}
}
}编辑:,在第二点中,它是否改变了性能?
发布于 2012-10-05 08:23:19
这并不意味着你应该在你能以一种有用的方式处理异常的地方捕获异常。
如果除了崩溃之外,您对异常无能为力,那么您的解决方案可以工作,但是考虑一下,例如,一个丢失的文件给了您一个异常。您是希望在"OpenFile“方法中使用对话框来处理它(或者在本例中可能是打开文件的方法的一部分),并可能给用户一个在继续之前浏览到文件所在位置的机会,还是希望它被抛回主目录,除了”日志和崩溃“没有真正的选项?
发布于 2012-10-05 10:52:05
这一办法:
一种更好的方法是在特定于上下文的方法中捕获预期的异常,在这种方法中,能够正确处理这些异常的知识最多。要捕获意外异常,您的主要方法可能如下所示:
// Event handler for handling all UI thread exceptions.
Application.ThreadException +=
new ThreadExceptionEventHandler(App_UiThreadException);
// Force all Windows Forms errors to go through our handler.
// NB In .NET 4, this doesn't apply when the process state is corrupted.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Event handler for handling all non-UI thread exceptions.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(App_NonUiThreadException);
// Run the application. https://stackoverflow.com/questions/12742267
复制相似问题