我知道try catch已经被讨论了很多次,但我还没有找到解决我的问题的方法。
我正在编写一个Silverlight应用程序,每个异常都应该生成一个MessageBox,显示类似“对不起造成的不便”这样的消息。
由于我不能保证我的代码不会出现异常,我的同事指示我在每个方法中(有几百个)都要像这样使用try catch:
public void Method1()
{
try
{
...
}
catch (Exception e)
{
MessageBox.Show("Something went wrong, we apologize for the inconvenience. \n" + e.Message);
}
}
public void Method2()
{
try
{
...
}
catch (Exception e)
{
MessageBox.Show("Something went wrong, we apologize for the inconvenience. \n" + e.Message);
}
}
但这看起来过于繁琐。我读过不要以这种方式使用try catch,而且会有很多重复的代码,代码也会变得晦涩难懂。
是否有任何替代方案,例如我可以使用全局try catch吗?
OutOfMemoryException
,你真的想尝试继续运行吗?)。 - Oded